我有以下对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');
}
});
当我尝试从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,例如请求“vendorId”等。要从任何jQuery ajax请求启用这种数据发布,您只需确保以下两点应用于您的jQuery ajax调用:
发布于 2012-12-31 06:19:37
选项1. 使服务器端代码保持相同的
首先删除kendo.stringify。然后删除contentType或将其更改为...
"application/x-www-form-urlencoded; charset=utf-8"
...or将您的$.ajax调用更改为:
$.post('DocSummaryDataAsync.aspx', { vendorId: supplierId, businessUnit: busUnit, productSegmentId: prodSegmentId, programId: progId, productManagerId: prodManagerId, companyIds: compIds, expired: exp.toString(), requestType: 'TotalCount' }, function (data) { });
选项2. 更改POST以获取
像这样
$.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、表单、Cookie和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();
}
https://stackoverflow.com/questions/14095041
复制相似问题