Home >Backend Development >PHP Tutorial >How to Efficiently Send Large JavaScript Arrays to PHP Scripts Using AJAX?
Sending Arrays to PHP Scripts Using Ajax
When dealing with large arrays in JavaScript, transmitting them efficiently to PHP scripts can be a challenge. The best approach is to utilize JSON (JavaScript Object Notation) for data transfer.
Solution:
const dataString = [1, 2, 3, 4, 5]; const jsonString = JSON.stringify(dataString);
$.ajax({ type: "POST", url: "script.php", data: { data: jsonString }, // JSON data sent as a key-value pair cache: false, success: function() { alert("OK"); } });
$data = json_decode(stripslashes($_POST['data'])); foreach ($data as $d) { echo $d; }
Note:
The above is the detailed content of How to Efficiently Send Large JavaScript Arrays to PHP Scripts Using AJAX?. For more information, please follow other related articles on the PHP Chinese website!