当我调用我的 webservice 方法时,出现以下错误。
Origin http://localhost:4165 is not allowed by Access-Control-Allow-Origin.
当参考网络时,我得到了像 add 这样的解决方案Access-Control-Allow-Origin
我不知道在哪里添加这个。
我的脚本是:
$(document).ready(function () {
$.ajax({
type: "Post", dataType: "json", contentType: "application/json; charset=utf-8",
url: "http://localhost:63384/ListWebService.asmx/HelloWorld", success: function (data) { alert(data.d); }, error: function (request, status, error) {
alert(request.responseText);
}
});
});
我的网络服务方法是:
[WebMethod]
public string HelloWorld()
{
return "Hello User";
}
我已经找到了我的问题的答案。只需将以下内容添加到您的 web.config 文件中
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
</customHeaders>
</httpProtocol>
</system.webServer>
另外,如果您不希望它在全局范围内设置,那么您可以将其单独添加到您的操作方法中,如下所示:
[WebMethod]
public string HelloWorld()
{
HttpContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
return "Hello User";
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)