jQuery和CORS

Ali*_*ned 1 ajax jsonp cors

如果服务器没有响应JSONP,我该如何处理?我已尝试使用$ .ajax的几个选项,但无法使其工作.

CodePen示例

//var url = 'https://coinbase.com/api/v1/currencies/exchange_rates';
var url = 'http://blockchain.info/ticker';
$.ajax({
    type: 'GET',
    url: url,
    //jsonpCallback: 'jsonCallback',
    contentType: 'application/json',
    //async: false,
    //dataType: 'jsonp',

    //xhrFields: { 
    // for CORS?
    //  withCredentials: false
    //},
    success: function (json) {
        debugger;
    },
    error: function (json) {
     debugger;
    }
});
Run Code Online (Sandbox Code Playgroud)

小智 5

看起来您正在尝试向blockchain.info发送AJAX请求并检索JSON提要.幸运的是,他们增加了对CORS的支持.

只需将参数传递cors=true给请求的URL(例如http://blockchain.info/ticker?cors=true),您就可以获取数据.

根据https://blockchain.info/api/blockchain_api,只支持几个API调用,看起来CORS看起来像.