admin管理员组

文章数量:1414605

I can't seem to make the response a json object.

the ajax function (url parameter to plupload) echoes the response like this:

echo json_encode(array(
  'foo'    => 3434,
  'error'  => 'omg error',
));

exit;

and in the FileUploaded event I'm evaluating that:

var json = eval('(' + response + ')');
console.log(json);  

But I get a error

Uncaught SyntaxError: Unexpected identifier

I can't seem to make the response a json object.

the ajax function (url parameter to plupload) echoes the response like this:

echo json_encode(array(
  'foo'    => 3434,
  'error'  => 'omg error',
));

exit;

and in the FileUploaded event I'm evaluating that:

var json = eval('(' + response + ')');
console.log(json);  

But I get a error

Uncaught SyntaxError: Unexpected identifier

Share Improve this question edited Nov 28, 2011 at 16:25 Pointy 414k62 gold badges595 silver badges629 bronze badges asked Nov 28, 2011 at 16:17 AlexAlex 66.2k185 gold badges460 silver badges651 bronze badges 4
  • can you post the json string? Are you sure "response" contains it? – rob Commented Nov 28, 2011 at 16:22
  • 2 ok, the problem was the function parameter, which is actually a object and response is a property :| like response.response – Alex Commented Nov 28, 2011 at 16:23
  • Is "pluload" in the question title supposed to be "upload"? – Pointy Commented Nov 28, 2011 at 16:23
  • no, it's the plupload script... – Alex Commented Nov 28, 2011 at 16:24
Add a ment  | 

2 Answers 2

Reset to default 4

Try to use jQuery parseJSON method.

var json = $.parseJSON(response);

As of v3.0 of jQuery, $.parseJSON(response) has been deprecated.

From the docs:

As of jQuery 3.0, $.parseJSON is deprecated. To parse JSON strings use the native JSON.parse method instead.

The answer to above question is thus:

var json = JSON.parse(response);

本文标签: javascriptplupload json responseStack Overflow