2016-08-24 115 views
2

我试图使用由网站提供的zoom.us API。他们给我卷曲命令来创建一个新用户:将cURL命令转换为ajax

curl --data 'api_key=your_api_key&api_secret=your_api_secret&[email protected]&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 protected]', '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. 
+2

POST数据和JSON数据是不一样的东西。你应该传递这个对象,而不要对它进行加密。也不要更改内容类型。 – apokryfos

+3

更常称为CORS(跨源资源共享)。如果你在每台客户端机器上放置API key/secrets,我会认为你做错了。 –

+0

感谢@apokryfos,解决了我的403错误,但现在我得到了一个新的405错误:XMLHttpRequest无法加载https://api.zoom.us/v1/user/create?api_key=key&api_secret = secret&email = test%40email.com&first_name =涓&姓氏=冈萨雷斯。请求的资源上没有“Access-Control-Allow-Origin”标题。 Origin'http://website.com'因此不被允许访问。该响应的HTTP状态码为405. – Juan

回答

0

由于@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); 
    } 
}); 

}

+0

我还建议您与API提供商沟通,让他们知道浏览器错误地将该Web服务的任何HTTP错误解释为CORS违规。这里没有CORS违规,只是远程服务器并不表示它正在接受CORS请求,所以浏览器认为错误是因为这个。 – apokryfos

1

Tr的y一旦添加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); 
    } 
});