asp.net – $.post vs $.ajax

前端之家收集整理的这篇文章主要介绍了asp.net – $.post vs $.ajax前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图使用$ .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"
);

我失踪了什么

解决方法

它不起作用,因为在$ .post方法中,您无法将请求的内容类型设置为application / json。所以不可能使用$ .post调用ASP.NET PageMethod,因为ASP.NET PageMethod需要一个JSON请求。你将不得不使用$ .ajax。

我只是修改数据,以确保它是正确的JSON编码:

$.ajax({
    type: "POST",data: JSON.stringify({ standardBagProductId: standardBagProductId.trim() }),success: function() {
        $(".reload").click();
    },contentType: "application/json"
});
原文链接:https://www.f2er.com/aspnet/252338.html

猜你在找的asp.Net相关文章