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调用:
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)
| 归档时间: |
|
| 查看次数: |
43987 次 |
| 最近记录: |