2012-02-01 79 views
1

我在使用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); 

昨天的工作,而且我找到了工作

有關我如何完成這項工作的任何建議?

+0

Eval是壞的。不惜一切代價避免它。另外,服務器返回到腳本的JSON看起來像什麼? – GordonM 2012-02-01 10:21:41

回答

1

檢查服務器返回的值並驗證該值它是有效的JSON(例如使用JSONLint)。

之後,您可以使用jQuery.parseJSON()將響應字符串轉換爲對象。

+0

是的,我發現了這一點,這就是我現在使用的。謝謝 :) – Steven 2012-02-01 11:40:34

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/

+0

這仍然不適用於IE或Chrome。 – Steven 2012-02-01 10:34:31

相關問題