Question

I have a simple webmethod on an asp.net 2.0 application (using the 1.0 extensions not the 3.5 ajax extensions). I'm attempting to call the method from jQuery and when I do it as the countless examples show on the Internet and here on SO, I get an Internal Server Error message returned.

Here's my current code:

[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public string Select(string login)
{
    UserProfile profile = UserProfile.GetUserProfile(login);

    return "{ FirstName: '" + profile.FirstName + "', " +
             "LastName: '" + profile.LastName + "', " +
             "EmailAddress: '" + profile.EmailAddress + "', " +
             "PhoneNumber: '" + profile.PhoneNumber + "' }";
 }

And now the jquery:

$.ajax({
    type: "POST",
    url: "Services/ProfileService.asmx/Select",
    dataType: "json",
    data: "{'login':'DOMAIN%5CUSER1'}",
    contentType: "application/json; charset=utf-8",
    success: function(msg){ alert(msg); },
    error: function(xhr){ alert(xhr.statusText);}     
});

The webservice is decorated with the [ScriptService] attribute as well. If I comment out the contentType, change the dataType to text, and change the data to be a query string (name=value), I get the XML returned appropriately.

Where am I going wrong?

UPDATE: I am using jQuery v1.3.1 and testing in both IE6 and Firefox 3. I'm getting consistent results.

Was it helpful?

Solution

This was a stupid mistake on my part. The issue has been resolved. While I included the reference to the AJAX Extensions, I forgot to rewrite the httphandler for ASMX services to the ScriptHandlerFactory class.

Adding this resolved the issue.

Since I have got some emails inquiring about examples or how I fixed the issue, I wrote a blog post about how to do this soup to nuts.

http://randomactsofcoding.blogspot.com/2009/03/jquery-json-and-asmx-20-services.html

OTHER TIPS

Yeap. You should abandon ASMX for WCF. Recommendation from Microsoft themselves. I guess it won't help you in your problem, but you would do well to consider WCF or at least start to look at that technology.

I had the same issue but it was fixed when i added the following to the code file

[System.Web.Script.Services.ScriptService]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top