【发布时间】:2018-07-20 11:35:20
【问题描述】:
我的 Vue.js 应用程序中有一个组件,它使用 Axios 将电子邮件从表单提交到 Mailchimp。
我已经读到要绕过 Mailchimp 的帖子 URL 中的 CORS,我需要使用 post-json 版本并将 &c=? 添加到 URL 的末尾。我还将我的请求方法从 POST 更新为 GET 并序列化了我的表单输入。
Component.vue
<mailchimp-subscribe action="https://example.us15.list-manage.com/subscribe/
post-json?u=xxx&id=xxx&c=?"></mailchimp-subscribe>
MailchimpSubscribe.vue
<template>
<form @submit.prevent="subscribe">
<input v-model="email" type="email" name="EMAIL" value="" placeholder="Email Address" required>
<input class="button" type="submit" value="Subscribe">
</form>
</template>
<script>
import axios from 'axios'
export default {
name: 'MailchimpSubscribe',
props: {
action: {}
},
data: () => ({
email: '',
response: {}
}),
methods: {
subscribe: function (event) {
axios({
method: 'get',
url: this.action,
data: JSON.stringify(this.email),
cache: false,
dataType: 'json',
contentType: 'application/json; charset=utf-8'
})
.then(response => {
console.log(response)
})
.catch(error => {
console.log(error)
})
}
}
}
</script>
使用上面的代码,我仍然得到以下错误:
无法加载https://example.us15.list-manage.com/subscribe/post-json?u=xxx&id=xxx&c=?:请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http://localhost:8080' 因此不允许访问。
有没有我遗漏的步骤?
【问题讨论】:
-
不,没有任何遗漏。将 URL 提交到您的服务器,然后使用服务器端 HTTP 客户端将请求发布到 Mailchimp。不能这样做。
-
@Ohgodwhy 你知道为什么它不再可能这样吗?我看过一些通过 $.ajax 提交到 mailchimp 的例子,我很好奇为什么我不能对 axios 做同样的事情。
-
@cul8r 因为
$.ajax支持jsonp(like this answer) 和axios does not support jsonp
标签: json forms vue.js axios mailchimp