I am trying to make an AJAX call using jQuery to a C# Web Service located under the same website. No matter what I find/try, I keep on receiving a 500 error as such:
Request format is unrecognized for URL unexpectedly ending in '/MyTest'.
jQuery AJAX call from web page
$.ajax({
type: "POST",
url: 'http://172.1.1.10/MYService/MyService.asmx/MyTest',
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data);
},
error: function (data, errorThrown) {
alert("Fail");
alert(errorThrown);
}
});
Web Service (MyService.asmx) Code Behind
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string MyTest()
{
return "Hello World";
}
and yes I have the following uncommented at the top:
[System.Web.Script.Services.ScriptService]
When I try to load my web page, I get the alert window saying “Fail”. This is when I receive the 500 error (see top of post).
Too many people suggested adding this to the web service’s web.config:
Web Service (Web.config)
<configuration>
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
</configuration>
Upon adding this, I can hit the local machine with this address:
http://172.1.1.10/MYService/MyService.asmx/MyTest
and I get the proper response of “Hello World”. Prior to adding this I would get the same 500 Error message locally.
Both the web service and web site are setup on the same IIS instance, each as its own application, under the same web site in IIS. I know it is not a cross-domain issue because I am not getting the 503 error that you’d typically receive if this were true, nor am I getting the 404 error of File Not Found.
I’ve scoured the web and cannot seem to find a resolution to this.
Thanks to nick_w, using Chrome’s Console feature provided me with the following error:
I discovered that when I was browsing to my site, I was using the domain name:
Whereas when I was calling the web service in code, I was using the IP Address:
Thus I technically was experiencing the cross-domain issue. Once I hit my site at the IP Address, or changed the webservice call to:
Then it resolved just fine. I did need to update my code as such:
Note, I added “.d” to the alert as it was returning an “Object object” rather then the result.