简体   繁体   中英

jQuery .ajax pass the whole JavaScript array to PHP

tdata = new Array();  
tdata['id'] = "helloid";  
tdata['name'] = "helloname";  
tdata['location'] = "hellolocation";  
tdata['about'] = "helloabout";  
tdata['company'] = "hellocompany";  
tdata['website'] = "hellowebsite";  

$.ajax({
    url: 'export.setsession.php',
        data: { tdata: tdata.id 
    },
    type: 'post',
    success: function (output) {
        //$(location).attr('href', 'index.php');
        alert("girdsposted");
    }
});

The above is working just fine but I would like to pass the array as a whole if that is possible like data: { tdata: tdata } as opposed to only passing the id. Is this possible to do or is there an alternative? I have not yet been able to get the whole array into PHP is some form I can read...

Thanks in advance...

Create a JSON string with:

JSON.stringify({ json: tdata });

and then convert it to a PHP array with:

json_decode($data);

您是否考虑过使用JSON.stringify()方法 ,该方法会将tdata元素转换为JSON字符串:

data: JSON.stringify(tdata)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM