2013-03-07 112 views
0

我不知道爲什麼我的代碼不能在屏幕上打印出鍵和值。警報消息是「[對象錯誤]」。從Ajax打印json值返回

請問讓我知道如何在屏幕上打印關鍵和價值巴黎?

的index.html

<script> 
$.ajax({ 
     type: "POST", 
     url: "image_finder.php", 
     data: dataString, 
     dataType: 'json', 
     error:function(xhr,status,e){  
      alert('Error'); 
     }, 
     success: function(data) { 
     $.each(data, function(key, val) { 
      alert(key); // problem 
     }); 


     }, 
     error:function(xhr, status, error) { 
      alert(error); 
     } 

    }); 
</script> 

JSON回報

test{"tags":[{"cid":"14","url":"http:\/\/localhost\/","img_url":"http:\/\/static.naver.net\/www\/up\/2013\/0305\/mat_173330634c.jpg","img_name":"mat_173317134c.jpg","html":"<div id=\"hotspot-19\" class=\"hs-wrap hs-loading\">\r\n<img src=\"http:\/\/static.naver.net\/www\/up\/2013\/0305\/mat_173330634c.jpg\">\r\n<div class=\"hs-spot-object\" data-type=\"spot\" data-x=\"95\" data-y=\"64\" data-width=\"30\" data-height=\"30\" data-popup-position=\"left\" data-visible=\"visible\" data-tooltip-width=\"200\" data-tooltip-auto-width=\"true\">\r\nasdf\r\n<\/div>\r\n<div class=\"hs-spot-object\" data-type=\"spot\" data-x=\"168\" data-y=\"53\" data-width=\"30\" data-height=\"30\" data-popup-position=\"left\" data-visible=\"visible\" data-tooltip-width=\"200\" data-tooltip-auto-width=\"true\">\r\nrere\r\n<\/div>\r\n<\/div>\r\n","jscript":""}]} 
+0

如果你的「json return」是成功的數據,你很可能想在數組上做$ .each(),而不是數據對象,即$ .each(data.tags,function(...){...}) – 2013-03-07 02:30:20

回答

-1

您從image_finder.php返回無效JSON - 尋找在image_finder.php流浪print 'test';echo 'test';

您可以使用像http://jsonlint.com/這樣的服務來驗證從後端腳本返回的JSON。當JSON無效時,jQuery將會出錯,這就是爲什麼Alert消息是[Object Error]