无法在JavaScript中调用api(跨源)
Unable to call an api in JavaScript (cross origin)
我写了下面的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.
我走的方向对吗?我如何解决这个问题?
谢谢。
正如在评论中提到的,Smartsheet API目前不支持CORS。
相关文章:
- 无法在Safari中加载跨源映像(来自CloudFront)
- 跨源请求阻止Spring REST服务+AJAX
- 每次都必须按 CTRL+f5 才能点击 api.如果不按它,就会弹出跨源请求阻止错误
- Twitter o嵌入跨源请求被阻止
- 在 ajax 中添加跨源标头,不起作用
- 如何将“跨源”标记添加到动态加载的脚本中
- Angularjs 跨源资源共享 (CORS) 发布或放置方法在 IE8 和 IE9 中不起作用
- 如何过滤推特流API订阅源,只获取与新闻相关的项目
- 如何访问 iframe.contentDocument 以在跨源请求后获得响应
- 跨源请求被POST阻止,但浏览器上的req可以正常工作,但设备上不工作
- 使用jQuery和Tornado实现跨源资源共享(CORS)
- Chrome上使用THRE.js的本地映像上的跨源映像加载被拒绝
- 我可以在子域中使用跨源资源吗
- 多个跨源请求
- 跨源请求被阻止 使用 javascript 向 API 发送帖子时出错
- 允许从 JavaScript 到 GlassFish 上的 SOAP Web 服务的跨源
- 如何使用food2fork api通过angularjs http进行跨源获取请求
- Expedia API 跨源请求
- 无法在JavaScript中调用api(跨源)
- Wolfram API javascript跨源共享问题