Maison > Article > développement back-end > Comment envoyer un grand tableau à un script PHP via AJAX ?
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}.
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!