簡體   English   中英

如何進行跨域Ajax調用API

[英]How can I make this cross-domain ajax call to an API

我遵循了有關跨域請求的教程(並閱讀了許多其他教程),但是無法正常工作。 我不斷收到相同的錯誤:

所請求的資源上沒有“ Access-Control-Allow-Origin”標頭。

這是我正在使用的代碼。 我正在嘗試使用coinbase API。

// Create the XHR object.
  function createCORSRequest(method, url) {
    var xhr = new XMLHttpRequest();
    if ("withCredentials" in xhr) {
      // XHR for Chrome/Firefox/Opera/Safari.
      xhr.open(method, url, true);
    } else if (typeof XDomainRequest != "undefined") {
      // XDomainRequest for IE.                                                                                                                                                         xhr = new XDomainRequest();
      xhr.open(method, url);                                                                                                                                                          } else {
      // CORS not supported.                                                                                                                                                            xhr = null;
    }                                                                                                                                                                                 return xhr;
  }

// Make the actual CORS request.
  function makeCorsRequest() {                                                                                                                                                        // All HTML5 Rocks properties support CORS.
    var url = 'https://www.coinbase.com/api/v1/prices/historical';
    var xhr = createCORSRequest('GET', url);                                                                                                                                          if (!xhr) {
      alert('CORS not supported');                                                                                                                                                      return;
    }                                                                                                                                                                                                                                                                                                                                                                   // Response handlers.
    xhr.onload = function() {
      var text = xhr.responseText;                                                                                                                                                      var title = getTitle(text);
      alert('Response from CORS request to ' + url + ': ' + title);
    };

    xhr.onerror = function() {
      alert('Woops, there was an error making the request.');
    };

    xhr.send();
  }

  makeCorsRequest();

默認情況下,不允許您進行跨域AJAX調用。 如果目標定義了CORS策略,則可以放寬此規則。 細節

如果您控制目標,則應該可以通過添加CORS策略使它起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM