"how to return an array from an ajax call?" Answer’s

0

Use JSON to transfer data types (arrays and objects) between client and server.

In PHP:

  • json_encode
  • json_decode

In JavaScript:

  • JSON.stringify
  • JSON.parse

PHP:

echo json_encode($id_numbers);

JavaScript:

id_numbers = JSON.parse(msg);

As Wolfgang mentioned, you can give a fourth parameter to jQuery to automatically decode JSON for you.

id_numbers = new Array();
$.ajax({
    url:"Example.php",
    type:"POST",
    success:function(msg){
        id_numbers = msg;
    },
    dataType:"json"
});
Wednesday, March 31, 2021
 
superfell
answered 11 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share