Convert js Array() to JSon object for use with JQuery .ajax

后端 未结 5 1555
慢半拍i
慢半拍i 2020-11-28 10:13

in my app i need to send an javascript Array object to php script via ajax post. Something like this:

var saveData = Array();
saveData[\"a\"] = 2;
saveData[\         


        
相关标签:
5条回答
  • 2020-11-28 10:15

    When using the data on the server, your characters can reach with the addition of slashes eg if string = {"hello"} comes as string = {\ "hello \"} to solve the following function can be used later to use json decode.

    <?php
    function stripslashes_deep($value)
    {
        $value = is_array($value) ?
                    array_map('stripslashes_deep', $value) :
                    stripslashes($value);
    
        return $value;
    }
    
    $array = $_POST['jObject'];
    $array = stripslashes_deep($array);
    
    $data = json_decode($array, true);
    print_r($data);
    ?>
    
    0 讨论(0)
  • 2020-11-28 10:23

    If the array is already defined, you can create a json object by looping through the elements of the array which you can then post to the server, but if you are creating the array as for the case above, just create a json object instead as sugested by Paolo Bergantino

        var saveData = Array();
        saveData["a"] = 2;
        saveData["c"] = 1;
        
        //creating a json object
        var jObject={};
        for(i in saveData)
        {
            jObject[i] = saveData[i];
        }
    
        //Stringify this object and send it to the server
        
        jObject= YAHOO.lang.JSON.stringify(jObject);
        $.ajax({
                type:'post',
               cache:false,
                 url:"salvaPreventivo.php",
                data:{jObject:  jObject}
        });
        
        // reading the data at the server
        <?php
        $data = json_decode($_POST['jObject'], true);
        print_r($data);
        ?>
    
        //for jObject= YAHOO.lang.JSON.stringify(jObject); to work,
        //include the follwing files
    
        //<!-- Dependencies -->
        //<script src="http://yui.yahooapis.com/2.9.0/build/yahoo/yahoo-min.js"></script>
    
        //<!-- Source file -->
        //<script src="http://yui.yahooapis.com/2.9.0/build/json/json-min.js"></script>
    

    Hope this helps

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

    You can iterate the key/value pairs of the saveData object to build an array of the pairs, then use join("&") on the resulting array:

    var a = [];
    for (key in saveData) {
        a.push(key+"="+saveData[key]);
    }
    var serialized = a.join("&") // a=2&c=1
    
    0 讨论(0)
  • 2020-11-28 10:41

    Don't make it an Array if it is not an Array, make it an object:

    var saveData = {};
    saveData.a = 2;
    saveData.c = 1;
    
    // equivalent to...
    var saveData = {a: 2, c: 1}
    
    // equivalent to....
    var saveData = {};
    saveData['a'] = 2;
    saveData['c'] = 1;
    

    Doing it the way you are doing it with Arrays is just taking advantage of Javascript's treatment of Arrays and not really the right way of doing it.

    0 讨论(0)
  • 2020-11-28 10:42

    There is actuly a difference between array object and JSON object. Instead of creating array object and converting it into a json object(with JSON.stringify(arr)) you can do this:

    var sels = //Here is your array of SELECTs
    var json = { };
    
    for(var i = 0, l = sels.length; i < l; i++) {
      json[sels[i].id] = sels[i].value;
    }
    

    There is no need of converting it into JSON because its already a json object. To view the same use json.toSource();

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