无法使用jQuery getJSON解释JSON

时间:2018-10-22 15:37:18

标签: jquery json

我对JSON和jQuery很陌生,所以我会尽力解释我不能做什么。我有这小块JSON数据


[
    {
        "id": 1,
        "name": "tratta 1",
        "type": "international",
        "from": "Bangkok",
        "to": "home",
    },
    {
        "id": 2,
        "name": "tratta 2",
        "type": "national",
        "from": "home",
        "to": "Rome",
    }
]



[
    {
        "id":1,
        "name":"Boeing 767-300",
        "lun":54.9 ,
        "wingspan":47.6, 
        "vel": 851,
        "vel max":913,
    },
    {
        "id":2,
        "name":"Boeing 737-800",
        "lun":33.4 ,
        "wingspan":35.8, 
        "vel": 840,
        "vel max":945,
    }
]

保存在我的磁盘上。我需要使用jQuery访问它以便在HTML上输出表,但目前我仅使用

登录

$.getJSON("js/test.json", function(data){ console.log(data); });


并且Firefox控制台说

  

解释错误XML:格式不正确

我该怎么办?

1 个答案:

答案 0 :(得分:1)

我认为您应该使用$ .ajax而不是getJSON,以便可以正确定义dataType。像这样的东西

   $.ajax({
    type: "GET",
    url: "js/test.json",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: "{}", //if you need to send data to server
    success: function(json) {
       console.log(json)
    },

});