2

我编写了以下 JavaScript 来调用 Smartsheet API:

$.get( "https://api.smartsheet.com/1.1/users/sheets", "Authorization: Bearer [My Access token]" )
.done(function( data ) {
    alert( "Data Loaded: " + data );
});

但这引发了以下错误:

XMLHttpRequest cannot load https://api.smartsheet.com/1.1/users/sheets?Authorization:%20Bearer%[My Access token]. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. 

经过一番阅读,我意识到代码必须发出跨域资源共享 (CORS) 请求。我从这里使用 jQuery 找到了以下代码:

function createCORSRequest(method, url) {
      var xhr = new XMLHttpRequest();
      if ("withCredentials" in xhr) {
        xhr.open(method, url, true);
      } else if (typeof XDomainRequest != "undefined") {
        xhr = new XDomainRequest();
        xhr.open(method, url);
      } else {
        xhr = null;
      }
      alert("cors created");
      return xhr;
}

var xhr = createCORSRequest('GET', "https://api.smartsheet.com/1.1/users/sheets?Authorization=Bearer+[My Access token]");
if (!xhr) {
    throw new Error('CORS not supported');
}
xhr.onload = function() {
    var text = xhr.responseText;
    alert('Response from CORS request: ' + text);
};
xhr.onerror = function() {
    alert('Woops, there was an error making the request.');
};
xhr.send();

但是,这再次在我的浏览器控制台中产生相同的错误:

XMLHttpRequest cannot load https://api.smartsheet.com/1.1/users/sheets?Authorization=Bearer+[My Access token]. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. 

我是否朝着正确的方向前进?我该如何解决这个问题?

谢谢。

4

1 回答 1

1

正如评论中提到的那样,Smartsheet API 目前不支持 CORS。

于 2014-08-12T17:16:26.187 回答