显示json返回的值

时间:2013-03-08 02:48:56

标签: ajax json

我有简单的问题在html页面上显示数据。以下代码在屏幕上显示json数据的数组。但是,我想通过“url”,“img_url”等各个元素来显示它。 你能不能让我知道该怎么做?

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 return

{"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":""}

1 个答案:

答案 0 :(得分: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
        }
    }
});

我希望这是你需要的。