2013-03-07 97 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]