Transmitting Arrays to PHP Scripts via Ajax
Problem:
An array populated using the ".push" function contains extensive data. How can this array be effectively sent to a PHP script?
Best Solution:
Sending the Array:
Encode the array into JSON format before sending it via Ajax.
var jsonString = JSON.stringify(dataString); $.ajax({ type: "POST", url: "script.php", data: {data : jsonString}, // Encode the data as a key-value pair cache: false, success: function(){ alert("OK"); } });
Receiving the Array in PHP:
Decode the encoded JSON string into an array.
$data = json_decode(stripslashes($_POST['data'])); foreach($data as $d){ echo $d; }
Note:
For POST requests, data should be sent as a key-value pair. Therefore, instead of data: dataString, use data: {data:dataString}.
Atas ialah kandungan terperinci Bagaimanakah saya menghantar Array Besar ke Skrip PHP melalui AJAX?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!