2017-11-11 201 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:意外的標記: –