【问题标题】:aspx and jquery.ajax is always returning an erroraspx 和 jquery.ajax 总是返回错误
【发布时间】:2011-06-17 20:45:19
【问题描述】:

这段代码在 mvc2 中运行良好,但又回到了传统的 ASPX(因为 Sharepoint 2010)。我遇到错误。谁能告诉我我在这个框架上做错了什么?

这个 ajax 调用在 $.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;
        }
    });

后面的代码

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

我每次都会出错。

【问题讨论】:

  • 你知道错误信息是什么吗?

标签: asp.net jquery webmethod


【解决方案1】:

您需要将 JSON 发送到服务并通过 contentType 标头表明您正在这样做:

$.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;
    }
});

更多信息在这里:http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

另外,如果您使用的是 jQuery 1.4,您可以删除 dataTypejQuery will infer JSON automatically based on the response's Content-Type header.

【讨论】:

    【解决方案2】:

    如果您将contentType 声明为 json 并且响应内容类型不是 json,那么 jQuery 中的 Ajax 调用将始终给您一个错误。如果您的 WebMethod 的响应有不同的内容(例如 html 或文本),您将始终收到该错误。您可以像这样在您的方法上设置该响应类型:

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

    在 WebMethods 之外也可以这样实现:

    Response.ContentType = "application/json";
    

    【讨论】:

    • 由于他正在调用页面方法,它确实需要 JSON 作为输入参数,并且它还会自动在响应中设置application/json Content-Type。无需手动操作。
    • 我认为如果您指定了它,则只需要 json 作为输入。 link。我们过去使用过pageMethods,并没有传入json
    • 如果没有参数,您可能已经摆脱了这种情况,但您必须使用带有静态“页面方法”和 ASMX“脚本服务”的 JSON 输入参数。如果您过去使用 ScriptManager 通过 PageMethod.YourMethodName() 语法调用页面方法,您可能没有注意到这一点。在这种情况下,MicrosoftAjax.js 会自动处理客户端 JSON 序列化。
    • 好的,那么当您向 decifer 发起 ajax 调用时,无论是普通页面提交回传还是 ajax 调用,最好的方法是什么?好像我的 ajax 调用和表单提交正在回发。我理解表单提交应该是,但我认为 ajax 调用不会在 Page_Load 事件中结束。任何代码示例?我的代码永远不会自动命中 WebMethods。您是否需要在 aspx 页面上有一个脚本管理器?
    • @Anthony:如果您不使用带有 Content-Type 应用程序/json 的 POST 请求,对页面方法的调用将在您的 Page_Load 事件中结束,因为它不会命中页面方法除此以外。您不需要 ScriptManager。您是否尝试过我答案中的代码?这是通过 jQuery 调用页面方法必须采用的方法。
    猜你喜欢
    • 1970-01-01
    • 2020-10-28
    • 2012-02-26
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    相关资源
    最近更新 更多