2013-03-08 114 views
0

我有简单的问题来显示html页面上的数据。以下代码在屏幕上显示json数据数组。但是,我想通过诸如“url”,“img_url”等每个元素来显示它。 您能否让我知道该做什么?json返回的显示值

Ajax代码

var dataString = 'url=' + pathname + '&img_name=' + img_name + "&tag=" + tag; 

    $.ajax({ 
     type: "POST", 
     url: "image_finder.php", 
     data: dataString, 
     dataType: 'json', 
     complete: function (xhr, status) { 
     if (status === 'error' || !xhr.responseText) { 
      //handleError(); 

      alert("error"); 

     } else { 
      var data = xhr.responseText; 



      $('#tt').html("<div id='message'></div>"); 
           $('#message').html(data); 



     } 
    } 
    }); 

JSON回报

{"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
$.ajax({ 
    type: "POST", 
    url: "image_finder.php", 
    data: dataString, 
    dataType: 'json', 
    success: function (data) { 
     for(var item in data){ 
      console.info(item);//print key 
      console.info(data[item]);//print value 
     } 
    } 
}); 

我希望这是你所需要的。

+0

它没有工作.... – 2013-03-08 05:05:04

+0

$。每次(数据,功能(键,值){ \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t $( '#tt')。html(“

”); \t \t \t $('#message')。html(key + “:”+ value); \t \t \t \t \t \t \t \t}); – 2013-03-08 05:16:24

+0

它打印出“1220:undefined” – 2013-03-08 05:16:50