我有以下jQuery AJAX調用ASP.Net頁面。jQuery AJAX調用將數據發佈到ASP.Net頁面(不是Get但POST)
$.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:斯蒂芬的回答是這最好的方法,特別是做的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代碼中進行訪問。
我也試過這種方式,任何東西都不適合我。誰能幫我。 – user1120998