Getting an array back from php using $.ajax
A php script is giving this array (which has been passed through json_encode()
)
[{name:"a1",path:"b1"},{name:"a2",path:"b2"}]
I use the following function to retrieve the array to jquery:
$.ajax({
type: "POST",
url: "functions.php",
data: "action=" + something,
cache: false,
success: function(response) { alert(response); }
});
The problem is I get the array back as a string:
(new String("[{name:"a1",path:"b1"},{name:"a2",path:"b2"开发者_如何学C}}]"))
How can I get it to be a javascript array?
Help would be much appreciated.
$.ajax({
type: "POST",
url: "functions.php",
dataType: 'json',
data: "action=" + something,
cache: false,
success: function(response) { alert(response); }
});
Try specifying the data type as JSON.
With JSON.parse()
:
function(response) { alert(JSON.parse(response)); }
精彩评论