admin管理员组

文章数量:1410682

I am uploading a file using PHP and want to return the file name and the file status to javascript. In PHP I create the json object by:

$value = array('result' => $result, 'fileName' => $_FILES['myfile']['name']);   
print_r ($value);
$uploadData = json_encode($value);

This creates the json object. I then send it to a function in javascript and recieve it as a variable called fileStatus.

alert (fileStatus);

It displays

{"result":"success","fileName":"cake"}

which should be good. But when I try and do

fileStatus.result or fileStatus.fileName 

I get an error saying that they are undefined. Please help I'm really stuck on this. Thanks.

I am uploading a file using PHP and want to return the file name and the file status to javascript. In PHP I create the json object by:

$value = array('result' => $result, 'fileName' => $_FILES['myfile']['name']);   
print_r ($value);
$uploadData = json_encode($value);

This creates the json object. I then send it to a function in javascript and recieve it as a variable called fileStatus.

alert (fileStatus);

It displays

{"result":"success","fileName":"cake"}

which should be good. But when I try and do

fileStatus.result or fileStatus.fileName 

I get an error saying that they are undefined. Please help I'm really stuck on this. Thanks.

Share Improve this question asked Aug 2, 2011 at 18:29 FalcataFalcata 7071 gold badge15 silver badges25 bronze badges 5
  • 4 fileStatus is probably the JSON string containing {"result":"success","fileName":"cake"}. – Gumbo Commented Aug 2, 2011 at 18:31
  • 2 Can you show the code where you set fileStatus? – gen_Eric Commented Aug 2, 2011 at 18:31
  • Guess you got some scoping problems, can you show the actual code? – Prusse Commented Aug 2, 2011 at 18:32
  • Are you correctly setting the document type? stackoverflow./questions/267546/… – Lance Commented Aug 2, 2011 at 18:33
  • Gumbo is correct. fileStatus is the JSON string. function stopUpload(fileStatus){ – Falcata Commented Aug 2, 2011 at 18:34
Add a ment  | 

3 Answers 3

Reset to default 3

The fileStatus is just a string at this point, so it does not have properties such as result and fileName. You need to parse the string into a JSON object, using a method such as Firefox's native JSON.parse or jQuery's jQuery.parseJSON.

Example:

var fileStatusObj = jQuery.parseJSON(fileStatus);

If the alert displays {"result":"success","fileName":"cake"} then you probably still have to turn the string into a JSON object. Depending on the browsers you are developing for you can use the native JSON support or the JSON implementation to turn your string into an object. From there on it should work as expected.

When you are setting the variable, do not put quotes around it. Just set the variable like this:

var fileStatus = <?php echo $uploadData; ?>;

or:

var fileStatus = <?=$uploadData?>;

Do not do this:

var fileStatus = '<?php echo $uploadData; ?>';

本文标签: phpJSON object quotundefinedquot error in JavascriptStack Overflow