2017-11-11 275 views
0

以下是我的代码:JSON API GET请求错误

$(document).ready(function(){ 
 
     $.ajax({ 
 
      url: 'https://bitconnect.co/api/info/BTC_BCC', 
 
      type: 'get', 
 
      dataType: 'json', 
 
      success: function(data){ 
 
       alert(data); 
 
      }, 
 
      error: function(error){ 
 
       alert(error); 
 
      } 
 
     }); 
 
    });
<html> 
 
     <head> 
 
      <meta charset="UTF-8"> 
 
      <title></title> 
 
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
      <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
      <script src = "//code.jquery.com/jquery-1.12.4.js"></script> 
 
    
 
      <script src="try.js"></script> 
 
     </head> 
 
     <body> 
 
      
 
     </body> 
 
    </html>

我想从AJAX的URL部分中提到的URL上的信息。 但我发现了以下错误:

Failed to load https://bitconnect.co/api/info/BTC_BCC: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. 

我完全新的这个部分,没有的错误是什么想法。 如果我能得到任何帮助,这将是一件好事。 在此先感谢。

+0

的[为什么我的JavaScript得到一个“不‘访问控制允许来源’标头出现在所请求的资源”错误,当邮差没有可能的复制?(https://开头stackoverflow.com/questions/20035101/why-does-my-javascript-get-a-no-access-control-allow-origin-header-is-present) – Isma

+0

是的,但我仍然没有得到那里给出的。 –

+0

你可以尝试使用代理url给我的解决方案,我已经测试过它,它的工作 –

回答

2

可以这样做,如果你是从本地主机做并使用这个应用程序的代理服务器,或者您也可以自行主机,并按照这个网址https://github.com/Rob--W/cors-anywhere/

var proxyUrl = 'https://cors-anywhere.herokuapp.com/' 

$.ajax({ 
    url: proxyUrl+'https://bitconnect.co/api/info/BTC_BCC', 
    type: 'get', 
    dataType: 'json', 
    crossDomain: true, 
    headers: { "Access-Control-Allow-Origin": "*" }, 
}).done(function(data) { 
    console.log(data); 
}); 
0

创建一个代理服务器您可以使用JSONP跨域请求

$.ajax({ 
    type: 'GET', 
    url: 'https://bitconnect.co/api/info/BTC_BCC', 
    async: false, 
    jsonpCallback: 'jQuery16206304910685867071_1380876031038', 
    contentType: "application/json", 
    dataType: 'jsonp', 
    success: function(json) { 
     console.dir(json.PRList); 
    }, 
    error: function(e) { 
     console.log(e.message); 
    } 
}); 
+0

jsonp抛出错误BTC_BCC?callback = jQuery16206304910685867071_1380876031038&_ = 1510404958275:1未捕获SyntaxError:意外的标记: –