20

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 を実行するアプローチです。ただし、Request["vendorId"] などの通常の従来の方法でデータを ASP.Net に投稿できるようにする小さなトリックを見つけました。jQuery ajax リクエストからのデータの投稿を有効にするには、次のようにする必要があります。次の 2 つの点が jQuery ajax 呼び出しに適用されていることを確認してください。

  1. content-type は jQuery ajax 呼び出しから除外する必要があります。または、それを含める場合は、「application/json; charset=utf-8」ではなく、「application/x-www-form-urlencoded」に設定する必要があります。 ; 文字セット=UTF-8". 私の理解によると、コンテンツタイプは、ページに期待されるデータのタイプではなく、送信されるデータのタイプをASP.Netページに伝えています。
  2. jQuery ajax のデータ部分では、データ名を引用符で囲んではなりません。したがって、data: {"venorId":"AD231","businessUnit":"123"} は data: {vendorId:"AD231",businessUnit:"123"} に置き換える必要があります。この例では、データ名は vendorId と businessUnit であり、Request["vendorId"] や Request["businessUnit"] などの通常の ASP.Net 構文を使用して、ASP.Net コード ビハインドでアクセスできます。
4

1 に答える 1

32

オプション 1.サーバー側のコードを同じに保つ

最初にkendo.stringifyを削除します。次に、contentType を削除するか、次のように変更します...

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

...または $.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 を GET に変更する

このような

$.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、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; }
}

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();
}
于 2012-12-30T22:19:36.867 に答える