2012-12-30 4 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"]; 
    } 

UPDATE 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"に変更してください。私の理解によると、Content-typeは、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" 

を...またはこれにあなたの$アヤックスの呼び出しを変更:

をGETする

$.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. 

同様

これは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

スティーブン - ありがとう。あなたは天才です。私はこの答えに100万ポイントを与えることができれば幸いです。それが私に必要なものを与えます。 – Sunil

+0

Stephen - "POST"タイプを "Get"に置き換えた場合、そのパラメータは名前がなくても、Request.Params [0]の最初の要素にデータがポストされます。だからaspxページのポストのようになりますか? – Sunil

+0

Stephen - なぜタイプですか:私がデータに送るデータを投稿してください:....? – Sunil

関連する問題