json返回数据在IE或Chrome中不起作用
我在使用Uploadify后返回JSON数据时遇到问题。json返回数据在IE或Chrome中不起作用
此代码适用于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);
昨天的工作,而且我找到了工作
有关我如何完成这项工作的任何建议?
检查服务器返回的值并验证该值它是有效的JSON(例如使用JSONLint)。
之后,您可以使用jQuery.parseJSON()
将响应字符串转换为对象。
是的,我发现了这一点,这就是我现在使用的。谢谢 :) – Steven 2012-02-01 11:40:34
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/
这仍然不适用于IE或Chrome。 – Steven 2012-02-01 10:34:31
Eval是坏的。不惜一切代价避免它。另外,服务器返回到脚本的JSON看起来像什么? – GordonM 2012-02-01 10:21:41