0

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"}}]"))

How can I get it to be a javascript array?

Help would be much appreciated.

2 Answers 2

3
$.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.

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

Comments

1

With JSON.parse():

function(response) { alert(JSON.parse(response)); }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.