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
3 Answers
Reset to default 3The 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
版权声明:本文标题:php - JSON object "undefined" error in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744996507a2636706.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论