json返回的数据无法在IE或Chrome中运行

时间:2012-02-01 10:16:05

标签: php json uploadify

使用Uploadify后返回JSON数据时遇到问题。

此代码适用于Firefox,但不适用于IE 9或Google Chrome 这是Uploadify的脚本脚本:

     jQuery("#uploadify_gallery").uploadify({
        'queueID'        : 'fileQueue',
        'uploader'       : siteURL + '/wp-content/plugins/uploadify/uploadify.swf',
        'script'         : siteURL + '/wp-content/plugins/uploadify/uploadify_gallery.php',
        'fileExt'        : '*.jpg;*.jpeg;*.png',
        'auto'           : true,
        'multi'          : true,
        'method'         : 'POST',
        'buttonImg'      : siteURL + '/wp-content/themes/storelocator/img/buttons/img_upload_grey_bg.png',
        'cancelImg'      : siteURL + '/wp-content/plugins/uploadify/cancel.png',
        'queueSizeLimit' : 20,
        'scriptData'     : {'entity':jQuery('#entity').val(),'entity_id':jQuery('#entity_id').val()},
        'onComplete'     : function(event, queueID, fileObj, response, data) {

          alert('test'); // <-- THIS WORKS 

          //This makes the json response readable
          var result = eval("(" + response + ")");
          alert(result.toSource()); // <-- this never fires
        },
      });

这是我在uploadify_gallery.php中测试的代码:

$res = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($res);

昨天工作了,我已经开始工作了

关于如何使这项工作的任何建议?

2 个答案:

答案 0 :(得分:1)

检查服务器返回的值并验证它是否是有效的JSON(例如,使用JSONLint)。

之后,您可以使用jQuery.parseJSON()将响应字符串转换为对象。

答案 1 :(得分:0)

的eval();它不是一个很好的选择,它被认为是非常糟糕的,因为它不能在Internet Explorer中工作,至少在旧版本中工作。看看这个

How can I get this eval() call to work in IE?

http://24ways.org/2005/dont-be-eval

您将获得响应作为json对象,因此不是使用eval,只需使用每个

循环它。
$(response).each(function(index, value) {
    console.log(value);
});

有关每种情况的详细信息,请查看http://api.jquery.com/each/