【问题标题】:Post request in JQuery Ajax在 JQuery Ajax 中发布请求
【发布时间】: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。

标签: jquery ajax cors box-api


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2011-06-02
    • 2014-12-08
    • 1970-01-01
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    • 2014-09-09
    相关资源
    最近更新 更多