调用Webservice方法时出现以下错误。
Origin http://localhost:4165 is not allowed by Access-Control-Allow-Origin.
当提到网络时,我正在获得像添加Access-Control-Allow-Origin 这样的解决方案,我不知道在哪里添加它。我的脚本是:
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"; }