$.ajax调用失败,dataType为json

$.ajax call fail with dataType is json

本文关键字:dataType json 失败 ajax 调用      更新时间:2023-09-26

我想向JSON文件发送一个ajax调用来检索数据。但是ajax调用失败,dataType: "json"。当我将dataType更改为"text"时,ajax调用成功。

下面是代码:

$.ajax({
    type: "POST",
    url: url,
    dataType: "json",
    success: function(response) {
        console.log("success");
    },
    error: function() {
        alert("failed");
    }
});

JSON文件为:

{
 "mobile": [{"中国": ["1", "2", "3", "4", "5", "6"]},
            {"美国": ["3", "5", "10", "20", "30", "50"]}
           ],
 "uni": [{"德国": ["5", "10", "20", "30", "50"]},
         {"英国": ["30", "50", "00", "20", "50"]}
        ],
 "telcom": [{"法国": ["10", "20", "30", "50", "00", "500"]}
           ]
}
您提供的

JSON是一个有效的JSON,所以我认为错误在其他地方
您可以使用分隔为dataType设置的多个值空间,如下所示:

dataType: "text json",

所以jquery将得到文本形式的结果,然后将其解释为JSON。

在评论中,你说有一些汉字,试着用Unicode编码。

这是因为数据类型不是JSON,而是application/json。尝试
    dataType: "application/json"

但如果这不起作用,您可以始终使用文本的dataType,然后使用JSON.parse,因为JSON就是这样。