Anthony
Anthony

Reputation: 536

aspx and jquery.ajax is always returning an error

This code worked fine in mvc2, but moving back to traditional ASPX (because of Sharepoint 2010). I am encountering errors. Can anyone tell me what I am doing wrong for this framework?

This ajax call is in the $.ready

$.ajax({
        type: "POST",
        dataType: "json",
        data: 'siteName=a&siteUrl=b',
        url: 'Wizard.aspx/DoesNameUrlExist',
        beforeSend: function () { alert("before send"); },
        complete: function () { alert("complete"); },
        success: function (data) { alert("success"); },
        error: function (data) {
            if ($("meta[name=debug]").attr("content") == "true") {
                //Full Error when debugging
                var errDoc = window.open();
                errDoc.document.write(data.responseText);
                errDoc.document.close();
            }
            else {
                // generic error message for production use
                alert("An unexpected error occurred.");
            } return false;
        }
    });

code behind

[WebMethod]
public static string DoesNameUrlExist(string siteName, string siteUrl)
{
    //do something
    return someString;
}

I get an error everytime.

Upvotes: 0

Views: 1263

Answers (2)

Dave Ward
Dave Ward

Reputation: 60590

You need to send JSON to the service and indicate that you're doing so via the contentType header:

$.ajax({
    type: "POST",
    contentType: 'application/json',
    data: '{"siteName":"a","siteUrl":"b"}',
    url: 'Wizard.aspx/DoesNameUrlExist',
    beforeSend: function () { alert("before send"); },
    complete: function () { alert("complete"); },
    success: function (data) { alert("success"); },
    error: function (data) {
        if ($("meta[name=debug]").attr("content") == "true") {
            //Full Error when debugging
            var errDoc = window.open();
            errDoc.document.write(data.responseText);
            errDoc.document.close();
        }
        else {
            // generic error message for production use
            alert("An unexpected error occurred.");
        } return false;
    }
});

More info here: http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

Also, if you're using jQuery 1.4, you can drop the dataType. jQuery will infer JSON automatically based on the response's Content-Type header.

Upvotes: 1

ashelvey
ashelvey

Reputation: 1355

Ajax calls in jQuery will always give you an error if you declare your contentType as json and the response content type is anything but json. If the response from your WebMethod has something different (such as html or text), you'll always get that error. You can set that response type on your method like this:

[WebMethod]
[ScriptMethod (UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public static string DoesNameUrlExist(string siteName, string siteUrl)

Outside of WebMethods this can also be achieved like this:

Response.ContentType = "application/json";

Upvotes: 0

Related Questions