【发布时间】:2014-01-20 09:16:45
【问题描述】:
您好,我正在使用 box api 进行集成,他们只提供了 curl 来获取访问令牌 如下
curl https://www.box.com/api/oauth2/token \
-d 'grant_type=authorization_code&code={your_code}&client_id={your_client_id}&client_secret={your_client_secret}' \
-X POST
我想使用 jquery ajax 请求获取访问令牌,我已经厌倦了我的代码,如下所示:
var data2= '{"grant_type":"authorization_code", "client_id":"READACTED" ,"client_secret":"Redacted" ,"code" :"'+code+'"}';
var data1 = JSON.parse(data2);
$.ajax({
type: 'POST',
url: "https://www.box.com/api/oauth2/token",
data: data1,
success: function(json) {
console.log(e);
alert("success"+json);
},
error: function(e) {
console.log(e)
alert("Failure"+ JSON.stringify(e));
}
});
我遇到了这个请求的问题,因为它在浏览器控制台中给了我错误:
XMLHttpRequest cannot load https://www.box.com/api/oauth2/token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
请告诉我哪里错了,它在 Postman 客户端上运行良好。
【问题讨论】:
-
你想做什么?你试过什么?
-
@RocketHazmat 请检查现在我提到了更多细节
-
你被same origin policy屏蔽了。您不能直接从 JavaScript 访问 box.com API。
-
我现在该怎么办>
-
您需要在您的服务器上创建一个脚本来联系 API,然后让您的 AJAX 调用指向该 API。