views:

539

answers:

4

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!

A: 

Does your class have the ScriptService attribute?? In C#:

[System.Web.Script.Services.ScriptService]
public class MyWebServiceClass
{
  ...
}

Otherwise use Firebug to check the error details and provide them here.

Juri
+1  A: 

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'}";
John Boker
I tried and no such luck.
Dan Appleyard
I had to create a custom httpmodule that would place the response from the webservice into a method call (JSONP) so it would work cross domains.
Dan Appleyard
+1  A: 

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.

Bela
A: 

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.

Alan