我有这段代码使用ajax消耗一个restful API。我只是试图在控制台上写下API返回的数据,令我惊讶的是它没有显示任何数据,有什么需要纠正的?
$.ajax({
type: "get",
url: "http://localhost:8800/dialects",
crossDomain: true,
dataType: 'jsonp',
success: function(data){
console.log(data);
}
});
当我直接从浏览器查询端点时,它返回了对象的端点:http://localhost:8800/dialects
。返回对象的示例:
[{"glottocode":"zyud1238","names":"Zyuzdin","isocodes":"","macroarea":"Eurasia"},{"glottocode":"zwal1238","names":"Zwall","isocodes":"","macroarea":"Africa"},{"glottocode":"zuwa1238","names":"Zuwadza","isocodes":"","macroarea":"Papunesia"},{"glottocode":"zuti1239","names":"Zutiua","isocodes":"","macroarea":"South America"},]
答案 0 :(得分:0)
尝试在ajax中使用content-type属性
$.ajax({
url: "http://localhost:8800/dialects",
type: "get",
crossDomain: true,
contentType: "application/jsonp; charset=utf-8",
dataType: 'jsonp',
success: function(data){
console.log(data);
}
});