0

Currently I have an ajax setup where my page with javascript calls a php file which then returns data via xhttp.responseText. This works great for strings but when I pass it a json encoded array, it still views that results as text. I get a string formatted version of my array that looks like this [1,2,3,4,5,6,7]

Which sort of works, but it's inconvenient because it's text representing an array and not an actual array.

How can I receive the data as an array instead of an array in text format? Currently I can still get the data using split(), but that seems sloppy. Is there an alternative to xhttp.responseText that would work better?

0

2 Answers 2

3

You can try sending your data as a JSON, which will give you all the flexibility in the kind of structure you want the data to have. PHP has the function json_encode which will convert your PHP variable into a JSON and you can receive this on the client side.

  1. First, make sure your PHP encodes the JSON as I said and that you have the proper header set too, i.e. header('Content-Type: application/json');

  2. Next, in your AJAX code, do JSON.parse(xhttp.responseText) to retrieve the JSON data as JavaScript object.

  3. Then you can do whatever you want with this object. Also, you can check your PHP is sending a JSON via xhttp.getResponseHeader("Content-type") which should return "application/json" if you are sending a JSON object.

Sign up to request clarification or add additional context in comments.

5 Comments

How do I receive it on the client side? I am currently using echo to send the json encoded array like so echo json_encode($send_array);
1. First, make sure your PHP encodes the JSON as I said and that you have the proper header set too, i.e. header('Content-Type: application/json'); 2. Next, in your AJAX code, do JSON.parse(xhttp.responseText) to retrieve the JSON data as JavaScript object. 3. Then you can do whatever you want with this object. Also, you can check your PHP is sending a JSON via xhttp.getResponseHeader("Content-type") which should return "application/json" if you are sending a JSON object.
@Terminus, I have moved my comment into my original answer.
@rby Ok, I've done that but the JSON.parse is not recognizing it as a valid JSON object. It throws a syntax error when I try running it on the responseText but works fine if I do a test such as JSON.parse('[1, 5, "false"]'); The only thing I can think of is my header because that's the main part I suspect I may not be using correctly. Do I just throw that at the top of the php file that' doing echo json_encode($send_array); ?
@user3225440, I suspect the problem is on your PHP side. Make sure that you are not echoing or printing anything else other than the data you are sending back, which should be echo json_encode(you data here); If you have other prints and echos in your PHP, all of those get sent back too and that will create a problem.
1

You need to set header, smth like following

$data = array('1', '2', '3');
header('Content-Type: application/json');
echo json_encode($data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.