curl ajax 区别,将cURL命令转换为ajax

我试图使用由网站提供的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.

我的问题是这样的:我做错了什么?我错误翻译了什么吗?此外,我知道类似的问题之前已经被问到(这是我如何提出我的翻译代码上面),但他们无法解决我的问题

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.

2016-08-24

Juan

+2

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

+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. –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想要使用 PHP 接收通过 AJAX 发送的文件,并通过 cURL 转发文件内容,可以按照以下步骤进行操作: 1. 前端 AJAX 请求: ```javascript var fileInput = document.getElementById('fileInput'); var file = fileInput.files[0]; var formData = new FormData(); formData.append('file', file); var xhr = new XMLHttpRequest(); xhr.open('POST', 'upload.php', true); xhr.onload = function() { if (xhr.status === 200) { console.log('文件上传成功'); } else { console.log('文件上传失败'); } }; xhr.send(formData); ``` 在上面的示例中,我们使用 `FormData` 对象来创建一个表单数据,并将文件添加到表单中。然后,我们使用 `XMLHttpRequest` 对象发送 POST 请求到 `upload.php` 文件,将表单数据作为请求体发送。 2. 后端 PHP 文件(upload.php): ```php <?php // 获取上传的文件 $file = $_FILES['file']; // 构建 cURL 请求 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://目标地址'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, [ 'file' => new CURLFile($file['tmp_name'], $file['type'], $file['name']) ]); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 发送 cURL 请求 $response = curl_exec($ch); curl_close($ch); // 输出 cURL 响应 echo $response; ?> ``` 在上面的 PHP 文件中,我们首先通过 `$_FILES['file']` 获取上传的文件信息。然后,我们使用 cURL 构建一个 POST 请求,设置目标地址、请求体(包含上传的文件),以及其他必要的选项。最后,我们使用 `curl_exec()` 发送 cURL 请求,并使用 `echo` 输出 cURL 的响应。 确保将目标地址替换为你实际的目标地址。 这样,当前端通过 AJAX 发送文件时,PHP 将接收文件并使用 cURL 将文件内容转发到目标地址。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值