部落冲突Ajax

时间:2018-11-22 22:24:24

标签: javascript jquery api jsonp

在此脚本中,尝试访问coc api信息时,出现以下错误:

Refused to execute script from 'https://api.clashofclans.com/v1/leagues?authorization=Bearer%20eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiIsImtpZCI6IjI4YTMxOGY3LTAwMDAtYTFlYi03ZmExLTJjNzQzM2M2Y2NhNSJ9.eyJpc3MiOiJzdXBlcmNlbGwiLCJhdWQiOiJzdXBlcmNlbGw6Z2FtZWFwaSIsImp0aSI6ImM4MjFmNzAxLWI2ZGQtNGQzZC05ODE1LTJjZmE1OGQxOTU0OCIsImlhdCI6MTU0MjgzMzIzMiwic3ViIjoiZGV2ZWxvcGVyL2YzNzc5ZjZhLTMwYzMtZmZjMS1kZmNlLWU2ZTYzY2VmMjQ2NSIsInNjb3BlcyI6WyJjbGFzaCJdLCJsaW1pdHMiOlt7InRpZXIiOiJkZXZlbG9wZXIvc2lsdmVyIiwidHlwZSI6InRocm90dGxpbmcifSx7ImNpZHJzIjpbIjE3Ni44NC4xNDQuMjA5Il0sInR5cGUiOiJjbGllbnQifV19.ftgHBIHMbP4oYGn1_6VR0LgU5gUCOCBgnRsp-4jeeFiGq1rLYwNtaj3rFSkRytyc38Z_GfRchCKuYTAcn2D6vw&callback=jQuery33105516973593012948_1542924782029&_=1542924782030' because its MIME type ('application/json') is not executable, and strict MIME type checking is enabled.
script.js:15 err: error

但是在开发人员工具->网络中,我可以在响应(一个json对象)中看到数据

怎么了?我需要通过代码访问数据

代码:

var key = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiIsImtpZCI6IjI4YTMxOGY3LTAwMDAtYTFlYi03ZmExLTJjNzQzM2M2Y2NhNSJ9.eyJpc3MiOiJzdXBlcmNlbGwiLCJhdWQiOiJzdXBlcmNlbGw6Z2FtZWFwaSIsImp0aSI6ImM4MjFmNzAxLWI2ZGQtNGQzZC05ODE1LTJjZmE1OGQxOTU0OCIsImlhdCI6MTU0MjgzMzIzMiwic3ViIjoiZGV2ZWxvcGVyL2YzNzc5ZjZhLTMwYzMtZmZjMS1kZmNlLWU2ZTYzY2VmMjQ2NSIsInNjb3BlcyI6WyJjbGFzaCJdLCJsaW1pdHMiOlt7InRpZXIiOiJkZXZlbG9wZXIvc2lsdmVyIiwidHlwZSI6InRocm90dGxpbmcifSx7ImNpZHJzIjpbIjE3Ni44NC4xNDQuMjA5Il0sInR5cGUiOiJjbGllbnQifV19.ftgHBIHMbP4oYGn1_6VR0LgU5gUCOCBgnRsp-4jeeFiGq1rLYwNtaj3rFSkRytyc38Z_GfRchCKuYTAcn2D6vw";
var url = "https://api.clashofclans.com/v1/leagues?authorization=" + key;
var data;

function loadLeagues() {
  $.ajax({
    url: url,
    dataType: "jsonp",
    success: function (res) {
      data = res;
      console.log("success: " + res)
    },
    error: function(err, msg) {
      console.log("err: " + msg)
    }
  });

}

1 个答案:

答案 0 :(得分:0)

键不是URL的一部分,而是标题的一部分。我对AJAX不熟悉,但也许像这样:

function loadLeagues() {
  $.ajax({
       url: 'https://api.clashofclans.com/v1/leagues',
       headers: {
           'Authorization': 'Bearer <your key>'
       },
       dataType: 'json',
       contentType: 'application/json',
       processdata: false,
       success: function(data) {
           alert(JSON.stringify(data));
       },

       ...

注意:还要仔细检查您的浏览器IP与您声明给Supercell的IP。