jQuery AJAX发布到asp.net webmethod永远不会被调用 [英] JQuery AJAX post to asp.net webmethod never getting called
问题描述
我的一个aspx页面中有一个Web方法:
I have a web method in one of my aspx pages:
[WebMethod]
public static string AddDebt(int userId, int type, string description, float amount)
在aspx页面中,我有了JQuery
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);
}
});
});
我知道我设置数据参数的方式看起来很愚蠢,但是以前它很干净,我将对其进行更改,但要点是,JSON对我来说似乎很好,不是吗?
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?
运行它时,没有发布到Web方法,尽管如果我更改contentType和dataType,我会得到整个aspx页面.我刚刚想到的一件事,比如说这个jquery实际上在register_borrower_step4.aspx页面上,这会引起问题吗?
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?
推荐答案
该方法不需要数组.尝试这样:
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);
}
});
还要根据amount
参数的区域性确保浮点分隔符正确.在.
和,
之间进行区分,否则Web方法可能不起作用.要进一步分析任何问题,您可以使用 FireBug 来了解幕后到底发生了什么.
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.
这篇关于jQuery AJAX发布到asp.net webmethod永远不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!