use*_*418 2 .net c# ajax jquery asp.net-web-api
我正在尝试使用Web API与Web窗体应用程序通过Ajax调用获取数据.
因此创建了一个Web API项目并将其部署到本地IIS.它回应了这些要求.

然后在webForm(Default.aspx)中进行Ajax调用以获取数据.
$(document).ready(function () {
var url = 'http://localhost:8080/api/Values';
$.ajax({
url: url,
type: 'POST',
crossdomain: 'true',
contentType: "application/json; charset=utf-8;",
type: 'GET',
success: function (result) {
var r = JSON.stringify(result);
alert("From Web-API " + r);
}
});
});
Run Code Online (Sandbox Code Playgroud)
但这是跟随错误
"XMLHttpRequest无法加载http:// localhost:8080/api/Values.请求的资源上没有'Access-Control-Allow-Origin'标头.因此不允许来源' http:// localhost:61605 '访问. "

我在这里错过哪个标题?
或者只需在您web.config的网络API站点中添加正确的标题:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)