tags:

views:

1586

answers:

2

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.

A: 

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.

John Leidegren
Thanks for the feedback and I normally would except for the fact the project requires the core 2.0 framework and not anything above that so no WCF. Trust me, I would like WCF but that decision is out of my hands at this time.
JamesEggers
Microsoft recommended WCF over asmx for web??? Thats strange considering the limitations WCF has with multiple host headers.
Mike_G
I don't think that System.Web.Script is available in the 2.0 framework.
hromanko
@hromanko System.Web.Script namespace is available through the ASP.Net AJAX Extensions v1.0 package for Asp.net 2.0 applications.
JamesEggers
A: 

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

JamesEggers