用JSON数据返回AJAX调用

时间:2014-12-02 16:08:55

标签: jquery ajax json

我已经设置了一个JSFiddle来了解延迟对象/ JSON / AJAX:http://jsfiddle.net/4HxSr/85/

我已经获得了代码,我用JSON数据成功发布了对JSFiddle的echo URL的请求,但是当我查看GET响应时,其中没有任何内容。我已经设法迭代JSON数组并显示元素及其属性,但我不确定我需要更改以从AJAX响应访问JSON数据。这可以在JSFiddle中练习吗?

这是来自小提琴的jQuery代码:

$(function () {

    var item = {
        "Stores": [{
            "Store": {
                "username": "matt",
                    "item1": "test1",
                    "Latitude": "-68.480280"
            }
        }, {
            "Store": {
                "username": "tim",
                    "item1": "test2",
                    "Latitude": "59.408408"
            }
        }, {
            "Store": {
                "username": "greg",
                    "item1": "test3",
                    "Latitude": "59.408408"
            }
        }]
    }

        function myAJAX() {
            return $.ajax({
                url: '/echo/json',
                type: 'POST',
                data: item,
                dataType: 'json',
                contentType: 'application/json'
            });
        }
        //fired when defferred object is resolved
        myAJAX().done(function (data) {
            var counter = 0;
            var html = '';
            $.each(item.Stores, function (index, item) {
                counter++;

                $.each(this, function (k, v) {
                    html += "<li>Store" + counter + " Username = " + this.username + "</li>";
                    html += "<li>Store" + counter + " item1 = " + this.item1 + "</li>";
                    html += "<li>Store" + counter + " Latitude = " + this.Latitude + "</li>";
                });
            });
            $('#post').html(html);
        })
            .fail(function () {
            console.debug("fail");
        });
});

0 个答案:

没有答案