Reputation: 35
Javascript has this fin line of code that alerts you what type of object you are working with. It is the following:
alert(Object.prototype.toString.apply(obj))
So, I am working with php
and js - extjs 4.2
, and looking for a way to pass an associative array
created by php to java by using json_encode()
. So I did the following:
<?php echo json_encode($some_array); ?>
Ext.Ajax.request(
{
url: 'php/parse.php?id='+buttonText,
method: 'GET',
success: function(response)
{
//var obj = response;
var obj = response.responseText;
alert(Object.prototype.toString.apply(obj));
},
failure: function(response)
{
alert('server-side failure with status code ' + response.status);
}
});
The php part of the code is in parse.php
file as you can see from js code. When I alert the obj, it shows a string of json encoded php array. And js says that obj is a string object
. If I keep the commented line without responseText, js alerts that obj is an Object
but does not say what type (guessing json type).
What i am trying to achieve, is to have a legit js array that will get that php array from response (response is the param. in the function). Thanks
EDIT : my php array structure
array
(
'vars' => array(
[0] => 'name' => '1', 'value' => 2
[1] => ... )
'file' => array(
[0] => 'name' => 'aaa.aa'
[1] => ...)
)
Upvotes: 0
Views: 350
Reputation: 888185
You're asking how to parse a string of JSON-encoded data into Javascript objects.
The built-in JSON.parse
function does exactly that.
Upvotes: 2