【问题标题】:Converting a cURL command into ajax [duplicate]将 cURL 命令转换为 ajax [重复]
【发布时间】:2016-12-31 19:21:53
【问题描述】:

我正在尝试使用该站点提供的 zoom.us API。他们给了我创建新用户的 cURL 命令:

curl --data 'api_key=your_api_key&api_secret=your_api_secret&email=user@email.com&type=1&first_name=John&last_name=Smith' https://api.zoom.us/v1/user/create

我翻译成 AJAX:

$.ajax({
        url: 'https://api.zoom.us/v1/user/create',
        type: "POST",
        cache: true,
        async: false,
        headers: {
            'Content-Type': 'application/json'
        },
        data: JSON.stringify({ 'api_key': 'key', 'api_secret': 'secret', 'email': 'email@email.com', 'first_name': 'John', 'last_name': 'Smith' }),
        success: function (res) {
            console.log(res);
        },
        error: function (err) {
            console.error(err);
        }
    });

(注意:'api_key' 和 'api_secret' 的变量只是上面示例中的占位符。我有自己的密钥和秘密,我在尝试进行此 API 调用时使用)

不过,此代码对我不起作用。我收到以下 403 错误:

XMLHttpRequest cannot load https://api.zoom.us/v1/user/create. 
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://mywebsite.com' is therefore not allowed access. The response had HTTP status code 403.

我的问题是:我做错了什么?有什么我翻译错了吗?另外,我知道以前有人问过类似的问题(这就是我想出上面翻译的代码的方式),但他们无法解决我的问题

这里是 zoom.us 文档以防万一:https://support.zoom.us/hc/en-us/articles/201363033-REST-User-API

ETA:apokryfos 发表评论后,这是我更新的代码:

$.ajax({
    url: 'https://api.zoom.us/v1/user/create',
    cache: true,
    async: false,
    data: { 'api_key': 'key', 'api_secret': 'secret', 'email': e, 'first_name': 'john', 'last_name': 'smith' },
    success: function (res) {
        console.log(res);
    },
    error: function (err) {
        console.error(err);
    }
});

产生一个新的 405 错误:

XMLHttpRequest cannot load api.zoom.us/v1/user/create?api_key=key&api_secret =secret&email=test%40email.com&first_name=Juan&last_name=Gon‌​zalez. 
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'website.com'; is therefore not allowed access. 

【问题讨论】:

  • POST 数据和 JSON 数据不是一回事。您应该传递对象而不对其进行攻击。也不要更改内容类型。
  • 通常称为CORS(跨域资源共享)。如果您将 api 密钥/秘密放在每台客户端机器上,我会认为您做错了。
  • 感谢 @apokryfos,它解决了我的 403 错误,但现在我得到了一个新的 405 错误:XMLHttpRequest cannot load api.zoom.us/v1/user/create?api_key=key&api_secret =secret&email=test%40email.com&first_name=Juan&last_name=Gonzalez。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'website.com' 不允许访问。响应的 HTTP 状态代码为 405。
  • 如果我的评论具有误导性,我们深表歉意。唯一需要删除的部分是 JSON 部分。 type: "POST" 应该保留。
  • @apokryfos 成功了!谢谢。

标签: php ajax curl


【解决方案1】:

尝试像下面这样添加 dataType: 'jsonp'

    $.ajax({
    url: 'https://api.zoom.us/v1/user/create',
    cache: true,
    dataType: 'jsonp'
    async: false,
    data: { 'api_key': 'key', 'api_secret': 'secret', 'email': e, 'first_name': 'john', 'last_name': 'smith' },
    success: function (res) {
        console.log(res);
    },
    error: function (err) {
        console.error(err);
    }
});

【讨论】:

    【解决方案2】:

    感谢@apokryfos,我能够找到正确的答案。

    $.ajax({
        url: 'https://crossorigin.me/https://api.zoom.us/v1/user/create',
        type: "POST",
        cache: true,
        async: false,
        data: { 'api_key': 'key', 'api_secret': 'secret', 'email': e, 'first_name': f, 'last_name': l },
        success: function (res) {
            console.log(res);
        },
        error: function (err) {
            console.error(err);
        }
    });
    

    }

    【讨论】:

    • 我还建议您与 API 提供商沟通,让他们知道浏览器错误地将来自该 Web 服务的任何 HTTP 错误解释为违反 CORS。这里没有违反 CORS,只是远程服务器没有表明它正在接受 CORS 请求,所以浏览器认为错误是因为这个。
    猜你喜欢
    • 2018-02-15
    • 2017-05-18
    • 2013-12-07
    • 1970-01-01
    • 2016-05-20
    • 2019-01-25
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多