Reputation: 426
I am getting the following error when I call my webservice method.
Origin http://localhost:4165 is not allowed by Access-Control-Allow-Origin.
When referring net I am getting solution like add Access-Control-Allow-Origin
I dont know where to add this.
My script is :
$(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);
}
});
});
And my webservice method is :
[WebMethod]
public string HelloWorld()
{
return "Hello User";
}
Upvotes: 3
Views: 18705
Reputation: 426
I have found the answer for my question. Just add the following to your web.config file
<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>
Also if you dont want it to be set up globally, then you can add it to your action method alone as below:
[WebMethod]
public string HelloWorld()
{
HttpContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
return "Hello User";
}
Upvotes: 12