开发者

aspx and jquery.ajax is always returning an error

开发者 https://www.devze.com 2023-03-13 15:30 出处:网络
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 wr开发者_开发技巧ong for this framework?

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 wr开发者_开发技巧ong 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.


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.


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";
0

精彩评论

暂无评论...
验证码 换一张
取 消