我正在尝试使用$.post方法来调用web服务,我已经使用$.ajax方法使其正常工作:
$.ajax({
type: "POST",
url: "StandardBag.aspx/RemoveProductFromStandardBag",
data: "{'standardBagProductId': '" + standardBagProductId.trim() + "' }",
success: function(){
$((".reload")).click();
},
dataType: "json",
contentType: "application/json"
});
但是,当我将相同的方法移到$.post方法中时,它将不起作用:
$.post("StandardBag.aspx/RemoveProductFromStandardBag",
"{'standardBagProductId': '" + standardBagProductId.trim() + "' }",
function () { $((".reload")).click(); },
"json"
);
我遗漏了什么?
发布于 2011-09-23 20:15:59
它不起作用,因为在您的$.post
方法中,您不能将请求的内容类型设置为application/json
。因此,不可能使用$.post
调用ASP.NET PageMethod,因为ASP.NET PageMethod需要JSON请求。您将不得不使用$.ajax
。
我只需修改data
,以确保其经过正确的JSON编码:
$.ajax({
type: "POST",
url: "StandardBag.aspx/RemoveProductFromStandardBag",
data: JSON.stringify({ standardBagProductId: standardBagProductId.trim() }),
success: function() {
$(".reload").click();
},
dataType: "json",
contentType: "application/json"
});
发布于 2015-06-16 06:34:29
这是另一种不使用ajax的方法。它使用post并返回一个json对象。
data = {};
data.standardBagProductId = standardBagProductId.trim();
$.post("StandardBag.aspx/RemoveProductFromStandardBag", data , function(response){
$(".reload").click();
},"json");
发布于 2011-09-23 20:15:48
for $.post函数的第二个参数不应在"“中。
$.post("StandardBag.aspx/RemoveProductFromStandardBag",
{'standardBagProductId': standardBagProductId.trim() },
function () { $(".reload").click(); },
"json"
);
https://stackoverflow.com/questions/7528757
复制相似问题