【问题标题】:Json does not write the returned object to the consoleJson 不会将返回的对象写入控制台
【发布时间】:2017-03-31 13:47:30
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    尝试在 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);
        }
    });
    

    【讨论】:

    • 谢谢。它也没有提取数据。我不知道可能是什么原因。我编写的同一行代码适用于我所做的另一个项目,所以我很困惑缺少什么。有什么想法吗?
    • 您能否详细介绍一下您正在使用的服务器 API 和中间件。
    猜你喜欢
    • 2014-02-17
    • 2014-08-17
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    相关资源
    最近更新 更多