jQuery AJAX调用将数据发布到ASP.Net页面(不是Get但是POST)

Sun*_*nil 20 asp.net jquery post

我有一个ASP.Net页面的以下jQuery AJAX调用.

             $.ajax({
                async: true,
                type: "POST",
                url: "DocSummaryDataAsync.aspx", //"DocSummary.aspx/GetSummaryByProgramCount",
                contentType: "application/json; charset=utf-8",
                data: kendo.stringify({ vendorId: supplierId, businessUnit: busUnit, productSegmentId: prodSegmentId, programId: progId, productManagerId: prodManagerId, companyIds: compIds, expired: exp.toString(), requestType: 'TotalCount' }),
                success: function (msg) {
                    // alert('in success of getcount');

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    // alert('in failure of getcount');


                }
            });
Run Code Online (Sandbox Code Playgroud)

当我尝试从Request对象中检索已发布的数据时,它不会显示出来.我的aspx页面代码如下.我将每个以Json格式发布的数据发送到页面,但它不会显示在页面的代码隐藏中.在我缺少的jQuery ajax调用中是否有一些额外的设置?

   protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "application/json";

        string requestType = Request.Params["requestType"];


        //populate variables from posted data
        string vendorId = Request.Params["vendorId"];
        string businessUnit = Request.Params["businessUnit"];
        string productSegmentId = Request.Params["productSegmentId"];
        string commitmentProgramId = Request.Params["programId"];
        string productManagerId = Request.Params["productManagerId"];
        string companyIds = Request.Params["companyIds"];
        string expired = Request.Params["expired"];
     }
Run Code Online (Sandbox Code Playgroud)

更新1: Stephen的回答是最好的方法,特别是ProcessRequest的方法.不过,我也发现一个小窍门,使数据被张贴到ASP.Net在通常的传统方式,即类似请求["VENDORID"]等.为了能够从任何jQuery的Ajax请求的数据,例如发布,您只需确保以下2点适用于您的jQuery ajax调用:

  1. 内容类型应该被排除在你的jQuery ajax调用之外或者如果你想要包含它,那么它不应该被设置为 "application/json; charset = utf-8"而是应用于"application/x-www-form-urlencoded" ; charset = UTF-8".根据我的理解,内容类型告诉ASP.Net页面正在发送的数据类型,而不是页面所需的数据类型.
  2. jQuery ajax的数据部分不应该用引号括起数据名.所以数据:{ "venorId": "AD231", "businessUnit": "123"}应由数据被替换为:{VENDORID: "AD231",businessUnit: "123"}.在此示例中,数据名称为vendorId和businessUnit,可以使用常用的ASP.Net语法(如Request ["vendorId"]和Request ["businessUnit"]在ASP.Net代码隐藏中访问).

Ste*_*uer 32

选项1. 保持服务器端代码相同

首先删除kendo.stringify.然后删除contentType或将其更改为...

"application/x-www-form-urlencoded; charset=utf-8" 
Run Code Online (Sandbox Code Playgroud)

...或者将$ .ajax调用更改为:

$.post('DocSummaryDataAsync.aspx', { vendorId: supplierId, businessUnit: busUnit, productSegmentId: prodSegmentId, programId: progId, productManagerId: prodManagerId, companyIds: compIds, expired: exp.toString(), requestType: 'TotalCount' }, function (data) { });
Run Code Online (Sandbox Code Playgroud)

选项2. 将POST更改为GET

像这样

$.ajax({
async: true,
type: "GET",
etc.
Run Code Online (Sandbox Code Playgroud)

这将通过QueryString传递您的数据.如果删除kendo.stringify调用,您将访问所有值,如下所示:

string vendorId = Request.QueryString[0];
string businessUnit = Request.QueryString[1];
etc.
Run Code Online (Sandbox Code Playgroud)

选项3. 使用原始的$ .ajax调用

如果您使用原始的$ .ajax,则以下内容适用:

Request.Params获得"QueryString,Form,Cookies和ServerVariables项目的组合集合".- 这个链接

你没有使用任何这些.相反,您需要访问Request.InputStream.

这是你如何做到这一点:

在服务器端创建一个映射到请求的JSON对象的类,例如

public class MyClass
{
    // The type (int or string) should probably correspond to the JSON
    public int vendorId { get; set; }
    public string businessUnit { get; set; }
    public string productSegmentId { get; set; }
    public string programId { get; set; }
    public string productManagerId { get; set; }
    public string companyIds { get; set; }
    public string expired { get; set; }
    public string requestType { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

将Request.InputStream转换为该类型,然后您可以使用它.

public void ProcessRequest()
{
    System.IO.Stream body = Request.InputStream;
    System.Text.Encoding encoding = Request.ContentEncoding;
    System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
    string json = reader.ReadToEnd();
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    MyClass myclass = (MyClass)serializer.Deserialize(json, typeof(MyClass));
    int vendorId = myclass.vendorId;
    string requestType = myclass.requestType;
    // etc...
}

protected void Page_Load(object sender, EventArgs e)
{
    ProcessRequest();
}
Run Code Online (Sandbox Code Playgroud)

  • 斯蒂芬 - 谢谢.你是个天才.我希望我能给你一百万分的答案.这给了我所需要的东西. (2认同)
  • Stepehen - 我发现有效的另一件事就是改变内容类型,所以它的==> contentType:"application/x-www-form-urlencoded; charset = UTF-8",//"application/json; charset = UTF-8" .然后在查询字符串中没有传递任何内容,但Request.Params [0]包含我传递的json字符串. (2认同)