首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用于将数据发布到ASP.Net页面的jQuery AJAX调用(不是Get而是POST)

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

Stack Overflow用户
提问于 2012-12-31 05:52:54
回答 1查看 45.3K关注 0票数 20

我有以下对ASP.Net页面的jQuery AJAX调用。

代码语言:javascript
复制
             $.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调用中是否有一些我遗漏的额外设置?

代码语言:javascript
复制
   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,例如请求“vendorId”等。要从任何jQuery ajax请求启用这种数据发布,您只需确保以下两点应用于您的jQuery ajax调用:

  1. The内容类型应该从您的jQuery ajax调用中删除,或者如果您想包括它,那么它不应该设置为/json;charset=UTF-8”,而是设置为“应用程序/x-www-form-urlencoded;charset=utf-8”。Content- type,根据我的理解,是告诉ASP.Net页面正在发送的数据类型,而不是页面预期的数据类型。
  2. jQuery ajax 的数据部分不应该将数据名称用引号括起来。所以数据:{"venorId":"AD231","businessUnit":"123"}应该替换为数据:{vendorId:"AD231",businessUnit:"123"}。在此示例中,数据名称为vendorId和businessUnit,可以在ASP.Net代码隐藏中使用常见的ASP.Net语法(如请求“vendorId”和businessUnit)来访问它们
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-31 06:19:37

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

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

代码语言:javascript
复制
"application/x-www-form-urlencoded; charset=utf-8" 

...or将您的$.ajax调用更改为:

代码语言:javascript
复制
$.post('DocSummaryDataAsync.aspx', { vendorId: supplierId, businessUnit: busUnit, productSegmentId: prodSegmentId, programId: progId, productManagerId: prodManagerId, companyIds: compIds, expired: exp.toString(), requestType: 'TotalCount' }, function (data) { });

选项2. 更改POST以获取

像这样

代码语言:javascript
复制
$.ajax({
async: true,
type: "GET",
etc.

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

代码语言:javascript
复制
string vendorId = Request.QueryString[0];
string businessUnit = Request.QueryString[1];
etc.

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

如果您使用原始$.ajax,则以下情况适用:

Request.Params获取“QueryString、表单、Cookie和ServerVariables项的组合集合”。- this link

您不能使用其中的任何一个。相反,您需要访问Request.InputStream。

下面是你如何做到这一点:

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

代码语言:javascript
复制
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转换为该类型,然后就可以使用它了。

代码语言:javascript
复制
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();
}
票数 32
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14095041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档