2012-12-30 31 views
17

我有以下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調用:

  1. 的內容類型應留出你的jQuery Ajax調用的或者,如果你想包括它,那麼它應該不能設置到「application/json; charset = utf-8」,但是到「application/x-www-form-urlencoded; charset = UTF-8」。按照我的理解,內容類型告訴ASP.Net頁面正在發送的數據的類型,而不是頁面預期的數據類型。
  2. jQuery ajax 的數據部分不應包含引號中的數據名稱。所以數據:{「venorId」:「AD231」,「businessUnit」:「123」}應該被數據替換:{vendorId:「AD231」,businessUnit:「123」}。在這個例子中,數據名稱是vendorId和businessUnit,可以使用通常的ASP.Net語法(如Request [「vendorId」]和Request [「businessUnit」])在ASP.Net代碼中進行訪問。
+0

我也試過這種方式,任何東西都不適合我。誰能幫我。 – user1120998

回答

30

選項1 讓您的服務器端代碼相同的

首先拆下kendo.stringify。然後,要麼刪除的contentType或將其更改爲...

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

...或者改變您的$就調用此:

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

選項2 更改POST到GET

像這樣

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

這將通過將q傳數據ueryString。如果刪除kendo.stringify叫你將訪問所有的值是這樣的:

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

選項3 如果使用原來的$就使用你原來的$就調用

,那麼以下適用:

請求。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(); 
} 
+2

Stephen - 謝謝。你是個天才。我希望我能給你一百萬分這個答案。這給了我我需要的東西。 – Sunil

+0

Stephen - 但如果我只是通過類型「Get」替換類型:「POST」,那麼數據會被髮送到Request.Params [0]的第一個元素,儘管該參數沒有名稱。那麼獲取像aspx頁面的Post? – Sunil

+0

Stephen - 爲什麼要輸入:獲取我發送的數據中的數據:....? – Sunil

相關問題