JQuery AJAX 发布到 asp.net webmethod 永远不会被调用
我的一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该方法不需要数组。请像这样尝试:
还要确保根据
amount
参数的区域性,浮点分隔符是正确的。请区分.
和,
,否则 Web 方法可能无法正常工作。要进一步分析任何问题,您可以使用 FireBug 来准确了解幕后发生的情况。The method doesn't expect an array. Try like this:
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.