在JQuery Ajax中发布请求

时间:2013-12-16 19:12:47

标签: jquery ajax cors box-api

您好我正在使用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客户合作正常。

1 个答案:

答案 0 :(得分:0)

为了超越同源政策,您需要联系Box支持团队。您可以发送电子邮件至api@box.com或点击右下方Box网页上的“Box Community”链接。

支持团队会在CORS标头中询问您要使用的原始URL或URL(Box不允许使用通配符)。他们还需要知道您的应用程序名称或API密钥以查找您的应用程序并为您设置CORS标头。