如何使用get方法跨域jquery ajax响应?

时间:2013-01-30 08:22:32

标签: jquery jsonp

如何使用jQuery或node.js的东西对json文件进行异步跨域请求(GET)? 我有我的api,我只需要向文件发出请求

$.ajax({
    type: 'GET',
    url: "https://api.flightstats.com/flex/airports/rest/v1/json/active?appId=3a311b8b&appKey=5d67203e8afab7fe8d01e0debf177171",
    processData: true,
    data: {},
    dataType: "json"

});

结果

XMLHttpRequest
XMLHttpRequest cannot load https://api.flightstats.com/flex/airports/rest/v1/json/active?appId=3a311b8b&appKey=5d67203e8afab7fe8d01e0debf177171. Origin https://developer.flightstats.com is not allowed by Access-Control-Allow-Origin.

$.ajax({
    type: 'GET',
    url: "https://api.flightstats.com/flex/airports/rest/v1/json/active?appId=3a311b8b&appKey=5d67203e8afab7fe8d01e0debf177171?callback?",
    processData: true,
    data: {},
    dataType: "jsonp"        
});

结果

undefined

2 个答案:

答案 0 :(得分:0)

您的网址回调函数缺少=。这是正确的网址。然后尝试一下。

  1. 确保您没有在控制台中获得任何404错误。
  2. 检查控制台是否存在任何其他错误。

    $.ajax({
        type: 'GET',
        url: "https://api.flightstats.com/flex/airports/rest/v1/json/active?appId=3a311b8b&appKey=5d67203e8afab7fe8d01e0debf177171?callback=?",
        processData: true,
        data: {},
        dataType: "jsonp",
        success:function(data){//do some thing with data},
        error:function(xhr, ajaxOptions, thrownError){//do with ajax errors}
    });
    

答案 1 :(得分:0)

试试这个:

$.ajax({
   type: 'GET',
   url: "https://api.flightstats.com/flex/airports/rest/v1/json/active?appId=3a311b8b&appKey=5d67203e8afab7fe8d01e0debf177171?callback=?",
   dataType: "jsonp",
   success:function(data){
       console.log(data);
   }      
});