【问题标题】:Javascript call to ASP.NET Page Method working on IE but not on Chrome / Firefox对 ASP.NET 页面方法的 Javascript 调用在 IE 上工作,但在 Chrome / Firefox 上不工作
【发布时间】:2012-03-30 12:23:35
【问题描述】:

我在 ASPX 页面中有以下代码,当单击 PayPal 图标时,该代码与 Page Method 连接并检索会话的 PayPal 令牌。这在IE中执行时完美运行,但在Chrome或Firefox中执行时,jQuery的$.ajax调用立即返回代码0的错误。

$(document).ready(function () {
            $("input.PayPal").click(function () {
                $.ajax({
                    type: "POST",
                    url: "MyCurrentPage.aspx/PayPal",
                    data: "{IdRecibo : " + $(this).attr("data-value") + "}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (result) {
                        if (result.d.Result == "Success") {
                            window.location.href = result.d.Redirect;
                        }
                        else
                            alert("El servicio de PayPal está temporalmente deshabilitado.");
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert(xhr.status);
                        alert(thrownError);
                    }
                });
            });

【问题讨论】:

  • 不知道是不是跟不寻常的url格式ListadoExpedienteEconomico.aspx/PayPal有关。我注意到 Chrome/FF somtimes 解析文件夹路径的方式与 IE 不同。
  • 我试过用 Fiddler 检查请求和返回值(毕竟 Page Method 调用是 Service 调用),返回值符合预期。
  • throwedError 有错误说明吗?
  • 无错误说明。只有代码 0。
  • 用chrome检查控制台,应该是报错。您可以做的另一件事是在要调试的脚本上放置一个断点,或者添加一个“调试器”; “成功”和“错误”一样

标签: jquery asp.net internet-explorer google-chrome pagemethods


【解决方案1】:

对代码稍作改动即可解决 Chrome 的问题。如果return false; 不是$.ajax 调用的最后一部分,则似乎未正确解析 ASP.NET 页面方法的 JSON 响应。

这是工作代码:

    $("input.PayPal").click(function () {
        $.ajax({
            type: "POST",
            url: '<%= Page.Request.Url.AbsolutePath + "/PayPal" %>',
            data: JSON.stringify({ IdRecibo: $(this).attr("data-value") }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: "false",
            timeout: 10000,
            success: function (result) {
                if (result.d.Result == "Success") {
                    window.location.href = result.d.Redirect;
                }
                else
                    alert("El servicio de PayPal está temporalmente deshabilitado.");
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
            }
        });
        return false;
    });

【讨论】:

    【解决方案2】:

    您的 JSON 看起来格式不正确。试试

    "{ 'IdRecibo': 'value'}"
    

    【讨论】:

    • 正如我所说,它确实可以在 Internet Explorer 中完美运行。页面方法调用是在 Chrome 和资源管理器中进行的。因此,数据已正确发送。
    • 取决于每个浏览器使用的序列化器......所以这个答案并没有错 - 甚至可能是正确的;-)
    • 但是fiddler抛出的结果在Chrome和Explorer中是一样的,所以Page Method肯定调用正确了吧?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    相关资源
    最近更新 更多