$。员额VS $就 [英] $.post vs $.ajax
问题描述
我试图使用$。员额方法来调用Web服务,我得到它的工作使用$阿贾克斯方式:
I'm trying to use the $.post method to call a web service, I've got it working using the $.ajax method:
$.ajax({
type: "POST",
url: "StandardBag.aspx/RemoveProductFromStandardBag",
data: "{'standardBagProductId': '" + standardBagProductId.trim() + "' }",
success: function(){
$((".reload")).click();
},
dataType: "json",
contentType: "application/json"
});
但是,当我将同样的方法到$ .post的方法,它不会工作:
But when I move the same method into the $.post method, it will not work:
$.post("StandardBag.aspx/RemoveProductFromStandardBag",
"{'standardBagProductId': '" + standardBagProductId.trim() + "' }",
function () { $((".reload")).click(); },
"json"
);
我在想什么?
What am I missing?
推荐答案
它不会因为你的 $。交
方法工作,你不能设置的内容类型请求应用程序/ JSON
。因此,它是不可能通过调用一个ASP.NET PageMethod的 $。交
,因为ASP.NET PageMethod的需要一个JSON请求。你将不得不使用 $。阿贾克斯
。
It doesn't work because in your $.post
method you cannot set the content type of the request to application/json
. So it is not possible to invoke an ASP.NET PageMethod using $.post
because an ASP.NET PageMethod requires a JSON request. You will have to use $.ajax
.
我只想修改数据
,以确保它是正确的JSON恩codeD:
I would just modify the data
in order to ensure that it is properly JSON encoded:
$.ajax({
type: "POST",
url: "StandardBag.aspx/RemoveProductFromStandardBag",
data: JSON.stringify({ standardBagProductId: standardBagProductId.trim() }),
success: function() {
$(".reload").click();
},
dataType: "json",
contentType: "application/json"
});
这篇关于$。员额VS $就的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!