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"
});

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....