jQuery AJAX Call for posting data to ASP.Net page ( not Get but POST)

后端 未结 1 1884
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-23 15:18

I have the following jQuery AJAX call to an ASP.Net page.

             $.ajax({
                async: true,
                type: \"POST\",
                        


        
相关标签:
1条回答
  • 2020-12-23 15:37

    Option 1. Keep your server side code the same

    First remove the kendo.stringify. Then either remove the contentType or change it to...

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

    ...or change your $.ajax call to this:

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

    Option 2. Change the POST to GET

    Like this

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

    This will pass your data via the QueryString. If you remove the kendo.stringify call you would access all the values like this:

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

    Option 3. Use your original $.ajax call

    If you use your original $.ajax, then the following applies:

    Request.Params gets a "combined collection of QueryString, Form, Cookies, and ServerVariables items." - this link

    You are not working with any of those. Instead, you need to access Request.InputStream.

    Here's how you can do that:

    Create a class on the server side which maps to the requested JSON object, e.g.

    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; }
    }
    

    Convert Request.InputStream into that type, and then you can use it.

    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();
    }
    
    0 讨论(0)
提交回复
热议问题