JQuery AJAX 发布到 asp.net webmethod 永远不会被调用

发布于 2024-09-15 10:09:43 字数 1425 浏览 11 评论 0原文

我的一个 aspx 页面中有一个 web 方法:

[WebMethod]
public static string AddDebt(int userId, int type, string description, float amount)

在 aspx 页面中,我有 JQuery

$(".addDebt").click(function (e) {
            e.preventDefault();
            var userId = $("[id$='txtUserId']").val();
            var type = $("[id$='ddlExistingDebtType']").val();
            var description = $("[id$='txtExistingDebtLender']").val();
            var amount = $("[id$='txtExistingDebtAmount']").val();

            var results = new Array();
            results.push({ userId: userId });
            results.push({ type: type });
            results.push({ description: description });
            results.push({ amount: amount });
            var dataString = JSON.stringify(results);
            $.ajax(
            {
                type: "POST",
                url: "register_borrower_step4.aspx/AddDebt",
                data: dataString,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {
                    $(".pDebtsTable").text(result);
                }
            });

        });

我知道它看起来很愚蠢,我设置数据参数的方式但它之前更干净,我会更改它,但重点是, JSON 对我来说似乎很好,所以不是吗?

当它运行时,不会发布到 web 方法,但如果我更改 contentType 和 dataType,我会返回整个 aspx 页面。我刚刚想到一件事,假设这个jquery实际上位于register_borrower_step4.aspx页面上,这会导致问题吗?

I have a web method in one of my aspx pages:

[WebMethod]
public static string AddDebt(int userId, int type, string description, float amount)

And in the aspx page I have the JQuery

$(".addDebt").click(function (e) {
            e.preventDefault();
            var userId = $("[id$='txtUserId']").val();
            var type = $("[id$='ddlExistingDebtType']").val();
            var description = $("[id$='txtExistingDebtLender']").val();
            var amount = $("[id$='txtExistingDebtAmount']").val();

            var results = new Array();
            results.push({ userId: userId });
            results.push({ type: type });
            results.push({ description: description });
            results.push({ amount: amount });
            var dataString = JSON.stringify(results);
            $.ajax(
            {
                type: "POST",
                url: "register_borrower_step4.aspx/AddDebt",
                data: dataString,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {
                    $(".pDebtsTable").text(result);
                }
            });

        });

I know that looks stupid the way I have set the data param but it was cleaner before and I will change it, but the point is, the JSON seems fine to me so it isn't that?

When it is run there is not post to the web method, though if I change contentType and dataType I get the whole aspx page returned. One thing I just thought of, say this jquery is actually on the register_borrower_step4.aspx page, would that cause a problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神回复 2024-09-22 10:09:43

该方法不需要数组。请像这样尝试:

var dataString = JSON.stringify({ 
    userId: userId, 
    type: type, 
    description: description, 
    amount: amount 
});

$.ajax({
    type: "POST",
    url: "register_borrower_step4.aspx/AddDebt",
    data: dataString,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (result) {
        $(".pDebtsTable").text(result);
    }
});

还要确保根据 amount 参数的区域性,浮点分隔符是正确的。请区分 .,,否则 Web 方法可能无法正常工作。要进一步分析任何问题,您可以使用 FireBug 来准确了解幕后发生的情况。

The method doesn't expect an array. Try like this:

var dataString = JSON.stringify({ 
    userId: userId, 
    type: type, 
    description: description, 
    amount: amount 
});

$.ajax({
    type: "POST",
    url: "register_borrower_step4.aspx/AddDebt",
    data: dataString,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (result) {
        $(".pDebtsTable").text(result);
    }
});

Also make sure that floating point separator is correct according to the culture for the amount parameter. Make the distinction between . and , or the web method might not work. To further analyze any problems you could use FireBug to see exactly what is happening under the covers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文