I’m trying to get a PHP array with AJAX and turn it into a JSON array.
Right now in the external file I’m echoing the php array with JSON encoding:
echo json_encode($palabras);
Then on the main file I get the response and assign it to variable “jsarray “;
success:function(data_response){
jsarray = data_response;
}});
However I can’t access jsarray as an array. How can I turn it into a proper array I can access?
If you are 100% certain you can trust the contents of
data_response, you can simply evaluate it.I don’t know what library you are using, but that should be something like
jsarray = eval(data_response.responseText);WARNING: This does pose a security risk if a third party can inject data into
data_response. Since it’s a simpleeval(), any javascript code can be executed.I believe that most modern webbrowsers provide a JSON library to work around this issue. You should be able to use
JSON.parse(data_response.responseText)in that case. However, that will not work on all browsers.