gpt4 book ai didi

forms - Axios POST 请求 (VueJS) 上的跨源请求被阻止

转载 作者:行者123 更新时间:2023-12-02 02:55:53 25 4
gpt4 key购买 nike

我尝试在表单提交时发送 POST 请求,但一直遇到 CORS 问题。我收到以下错误:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://example.com/api/lists/contacts.
(Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://example.com/api/lists/contacts.
(Reason: CORS request did not succeed).

这是我的代码:

<template>
<form @submit.prevent="subscribeEmail" method="post" enctype="multipart/form-data" action="">
<input type="email" v-model="email" name="user_email">
<button type="submit" name="button">Subscribe</button>
</form>
</template>

<script>
import axios from 'axios';

export default {
methods: {
subscribeEmail() {
axios({
method: 'POST',
url: `https://example.com/api/lists/${id}/contacts`,
data: {
"api_key": apiKey,
"email_address": this.email,
},
headers: {
'Access-Control-Allow-Origin': '*',
'Content-type': 'application/json',
}
})
.then(function (response) {
console.log(response)
})
.catch(function (error) {
console.log(error);
});
},
},
}
</script>

此外,这是我的请求的网络描述。由于某种原因,该方法是选项... enter image description here

我不知道问题出在哪里。我尝试了很多事情。我真的很感激任何帮助或建议。提前非常感谢。

注意:此问题在本地主机和生产版本 (Netlify) 上都会发生。

最佳答案

事实证明,出于安全原因,我专门使用的电子邮件营销服务不允许客户端 API 调用。

如果您遇到类似问题,请尝试联系提供 API 的服务。他们也许能提供帮助。

要绕过此问题,您可以使用的一种方法是通过代理定向请求。最流行的解决方案是使用 https://cors-anywhere.herokuapp.com/ 。您基本上可以将此网址附加到您的请求中,如下所示:

axios({
method: 'POST',
url: `https://cors-anywhere.herokuapp.com/https://example.com/api/action`,
data: {
"api_key": apiKey,
"email_address": this.email,
},
headers: {
'Access-Control-Allow-Origin': '*',
'Content-type': 'application/json',
}
})

希望这有帮助。

关于forms - Axios POST 请求 (VueJS) 上的跨源请求被阻止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61150129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com