【问题标题】:Client call JSON result show as undefined客户端调用 JSON 结果显示为未定义
【发布时间】:2017-10-17 01:03:19
【问题描述】:

我有一组用json_decode函数在php中生成的json数据,结果如下:

然后我创建一个 html 文档并尝试使用 jquery $.getJSON 调用结果:

var apiSrc = 'http://localhost/api/ws-data';
var showData = $('#result');

$(function(){

    $.getJSON(apiSrc, function(data) {
        console.log(data);

        var items = data.blog.map(function (item) {
            return item.key + ': ' + item.value;
        });

        showData.empty();

        if(items.length) {
            var content = '<li>' + items.join('</li><li>') + '</li>';
            var list = $('<ul />').html(content);
            showData.append(list);
        }
    });

    showData.text('Loading...');
});

上面的结果是:

REST - Get JSON from PHP file on the server side

    undefined: undefined
    undefined: undefined
    undefined: undefined
    undefined: undefined
    ..

keyvalue 显示为undefined: undefined

脚本出了什么问题?

【问题讨论】:

  • 我在您的对象中看不到“键”和“值”属性。我看到 item.pid、item.category、item.title 等

标签: javascript jquery json client getjson


【解决方案1】:

我认为您应该访问正确的properties,例如pid,category等回复,

  var items = data.blog.map(function (item) {
            return item.pid + ': ' + item.category;
 });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 2016-04-08
    • 1970-01-01
    • 2016-05-11
    • 2021-04-16
    相关资源
    最近更新 更多