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

我有以下jQuery AJAX调用ASP.Net页面
$.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');


                }
            });

当我尝试从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"];
     }

更新1:Stephen的答案是最好的方法,特别是ProcessRequest的方法。然而,我确实发现了一个小技巧,它允许以通常的传统方式将数据发布到ASP.Net,例如Request [“vendorId”]等。为了使任何jQuery ajax请求能够发布数据,您只需要确保以下2点应用于您的jQuery ajax调用

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

解决方法

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

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

"application/x-www-form-urlencoded; charset=utf-8"

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

$.post('DocSummaryDataAsync.aspx',{ vendorId: supplierId,requestType: 'TotalCount' },function (data) { });

选项2.将POST更改为GET

喜欢这个

$.ajax({
async: true,type: "GET",etc.

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

string vendorId = Request.QueryString[0];
string businessUnit = Request.QueryString[1];
etc.

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

如果您使用原来的$ .ajax,则适用以下条件:

Request.Params获得“QueryString,Form,Cookies和ServerVariables项目的组合集合”。 – this link

你不和任何人一起工作。相反,您需要访问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; }
}

将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();
}

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: <span id=&quot...
jQuery 添加水印 <script src="../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...