weixin_33736832 2015-03-21 22:13 采纳率: 0%
浏览 25

AJAX调用无法解析JSON

I have the following piece of JSON:

[
{
    "number": "0",
    "name": "Russell Westbrook",
    "attemptedFG": [
        {
            "x": "333",
            "y": "97",
            "made": "true",
            "assisted": "false"
        },
        {
            "x": "571",
            "y": "389",
            "made": "true",
            "assisted": "false"
        }
    ],
    "attemptedFT": [
        {
            "made": "true"
        },
        {
            "made": "false"
        }
    ],
    "rebounds": "5",
    "assists": "8",
    "steals": "2",
    "blocks": "1",
    "turnovers": "3",
    "fouls": "4"
}
]

and I'm trying to parse it with this AJAX call using ReactJS:

loadStatsFromServer: function() {
    $.ajax({
        url: this.props.url,
        dataType: 'json',
        success: function(data) {
            this.setState({data: data});
        }.bind(this),
        error: function(xhr, status, err) {
        console.error(this.props.url, status, err.toString());
        }.bind(this)
    });
}

All the values in the JSON are still stringified after the AJAX call succeeds. The server sends back a response header with 'Content-Type', 'application/json' so I'm not sure why it's not parsing.

Server-side call:

app.get('/stats.json', function(req, res) {
    fs.readFile('stats.json', function(err, data) {
        res.setHeader('Content-Type', 'application/json');
        res.send(data);
    });
});

Thanks a bunch.

  • 写回答

1条回答 默认 最新

  • George_Fal 2015-03-22 12:56
    关注

    When you send a stringified json object from server to client, you need to parse that object to readable javascript object. So, you need to parse json in success event of ajax request:

    success: function(data) {
            var result = $.parseJSON(data);
        }
    

    Now result is a javascript readable object.

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘