Post an object as data using Jquery Ajax

后端 未结 6 2065
栀梦
栀梦 2020-11-28 10:24

My code that I tried is as follows:

var dataO = new Object();
dataO.numberId = 1;
dataO.companyId = 531;

$.ajax({
 type: \"POST\",
 url: \"TelephoneNumbers.         


        
相关标签:
6条回答
  • 2020-11-28 10:56

    Is not necessary to pass the data as JSON string, you can pass the object directly, without defining contentType or dataType, like this:

    $.ajax({
        type: "POST",
        url: "TelephoneNumbers.aspx/DeleteNumber",
        data: data0,
    
        success: function(data)
        {
            alert('Done');
        }
    });
    
    0 讨论(0)
  • 2020-11-28 10:56

    [object Object] This means somewhere the object is being converted to a string.

    Converted to a string:

    //Copy and paste in the browser console to see result
    
    var product = {'name':'test'};
    JSON.stringify(product + ''); 
    

    Not converted to a string:

    //Copy and paste in the browser console to see result
    
    var product = {'name':'test'};
    JSON.stringify(product);
    
    0 讨论(0)
  • 2020-11-28 10:58

    All arrays passed to php must be object literals. Here's an example from JS/jQuery:

    var myarray = {};  //must be declared as an object literal first
    
    myarray[fld1] = val;  // then you can add elements and values
    myarray[fld2] = val;
    myarray[fld3] = Array();  // array assigned to an element must also be declared as object literal
    

    etc...`

    It can now be sent via Ajax in the data: parameter as follows:

    data: { new_name: myarray },
    

    php picks this up and reads it as a normal array without any decoding necessary. Here's an example:

    $array = $_POST['new_name'];  // myarray became new_name (see above)
    $fld1 = array['fld1'];
    $fld2 = array['fld2'];
    etc...
    

    However, when you return an array to jQuery via Ajax it must first be encoded using json. Here's an example in php:

    $return_array = json_encode($return_aray));
    print_r($return_array);
    

    And the output from that looks something like this:

    {"fname":"James","lname":"Feducia","vip":"true","owner":"false","cell_phone":"(801) 666-0909","email":"jp@gmail.com", "contact_pk":"","travel_agent":""}
    

    {again we see the object literal encoding tags} now this can be read by JS/jQuery as an array without any further action inside JS/JQuery... Here's an example in jquery ajax:

    success: function(result) {
    console.log(result);
    alert( "Return Values: " + result['fname'] + " " + result['lname'] );
    }
    
    0 讨论(0)
  • 2020-11-28 10:58

    You may pass an object to the data option in $.ajax. jQuery will send this as regular post data, just like a normal HTML form.

    $.ajax({
        type: "POST",
        url: "TelephoneNumbers.aspx/DeleteNumber",
        data: dataO, // same as using {numberId: 1, companyId: 531}
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            alert('In Ajax');
        }
    });
    
    0 讨论(0)
  • 2020-11-28 11:12

    I will leave my original answer in place but the below is how you need to approach it. (Forgive me but it is a long time since I have used regular asp.net / web services with jquery:)

    You need to use the following js lib json2 library, you can then use the stringify method to ensure your json is in the correct format for the service.

    var data0 = {numberId: "1", companyId : "531"};
    
    var json = JSON2.stringify(data0 ); 
    
    $.ajax({
     type: "POST",
     url: "TelephoneNumbers.aspx/DeleteNumber",
     data: json,
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function(msg) {
     alert('In Ajax');
     }
    });
    

    UPDATE: Same issue / answer here

    0 讨论(0)
  • 2020-11-28 11:12

    Just pass the object as is. Note you can create the object as follows

    var data0 = {numberId: "1", companyId : "531"};
    
    $.ajax({
     type: "POST",
     url: "TelephoneNumbers.aspx/DeleteNumber",
     data: dataO,
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function(msg) {
     alert('In Ajax');
     }
    });
    

    UPDATE seems an odd issue with the serializer, maybe it is expecting a string, out of interest can you try the following.

    data: "{'numberId':'1', 'companyId ':'531'}",

    0 讨论(0)
提交回复
热议问题