Reputation: 7445
I am finding many "solutions" to this seemingly common problem, but alas, none seem to work for me - of course. I have some jQuery code that is trying to post to an ASP.NET 3.5 web service. The web service is supposed to return some json. I am getting 501 Internal Server errors with the code below, but according to the solutions found online, this is how it is supposed to be! The error function in the jquery code displays a parsererror. If I change how the data parameters are passed to the service ("userID=3456"
), and remove the contentType property and change the dataType to "text", then no errors, but it returns the JSON in an XML string.
Here is my jQuery code:
$.ajax({
type: "POST",
url: "mywebservice.asmx/Initialize",
data: "{'userID': '3456'}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(result) {
alert("success");
},
error: function(request, status, errorThrown) {
alert(status);
alert(errorThrown);
}
});
Here is my ASP.NET code:
<WebMethod()> _
Public Function Initialize(ByVal userID As Integer) As String
Dim scormInstance As New CMI(userID)
Return scormInstance.ToJsonString
End Function
Here are the HTTP request and response through my browser (Firefox 3.5.1)
http://localhost/cognition/webservices/cognitionapi.asmx/Initialize
POST /cognition/webservices/cognitionapi.asmx/Initialize HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1 (.NET CLR 3.5.30729)
Accept: application/json, text/javascript, */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Content-Type: application/json; charset=utf-8
X-Requested-With: XMLHttpRequest
Referer: http://localhost/cognition/scorm-test2.html
Content-Length: 20
Cookie: RadEditorGlobalSerializeCookie=[_ctl0__ctl0__ctl0_Content_mainContent_mainContent_txtSpecialInstructionsModules]-[]#[_ctl0__ctl0__ctl0_Content_mainContent_mainContent_txtSpecialInstructionsToolbars]-[]#
Pragma: no-cache
Cache-Control: no-cache
{"userID":"1234"}
HTTP/1.x 500 Internal Server Error
Cache-Control: private
Content-Length: 4956
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/7.0
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Mon, 27 Jul 2009 21:24:41 GMT
Any ideas as to what exactly I am doing wrong????!!!!???
Thanks!
Upvotes: 1
Views: 1558
Reputation: 241
Your webservice is expecting an int parameter, therefore you need to send a number value. Remove the quotes off of 3456. Check out json.org for type formats.
Upvotes: 0
Reputation: 83699
After looking at some code i have that's working i found this decoration on the function:
[WebMethod(), ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
also the postdata im passing in is in Json format, ie:
var postdata = "{'variable':'value'}";
Upvotes: 2
Reputation: 3464
Have you tried using a debugger and seeing if that happens to catch something? I believe some reasons for ajax call failures will show up when you have a debugger attached but you don't get much useful information on the page itself.
The event log may also have something useful in it.
Upvotes: 1