加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – $.post vs $.ajax

发布时间:2020-12-16 00:27:56 所属栏目:asp.Net 来源:网络整理
导读:我试图使用$ .post方法来调用Web服务,我已经使用$ .ajax方法工作了: $.ajax({ type: "POST",url: "StandardBag.aspx/RemoveProductFromStandardBag",data: "{'standardBagProductId': '" + standardBagProductId.trim() + "' }",success: function(){ $(("
我试图使用$ .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"
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读