Json不会将返回的对象写入控制台

时间:2016-11-17 06:06:54

标签: json ajax rest laravel-5.3 laravel-blade

我有这段代码使用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"},]

1 个答案:

答案 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);
    }
});