如何使用 mailchimp API 消除 CORS 错误?
how to get rid of CORS error with mailchimp API?
如何使用 mailchimp 处理 CORS 错误 API?
或者这段代码有什么问题:
这是使用 mailchimp 包:
import mailchimp from '@mailchimp/mailchimp_marketing'
mailchimp.setConfig({
apiKey: process.env.NEXT_PUBLIC_MAILCHIMP_API_KEY,
server: process.env.NEXT_PUBLIC_MAILCHIMP_DC,
});
try {
const response = await mailchimp.lists.addListMember(process.env.NEXT_PUBLIC_MAILCHIMP_LIST_ID, {
email_address: email,
status: "pending",
});
catch(err){}
这是使用 axios
const url = `https://${process.env.NEXT_PUBLIC_MAILCHIMP_DC}.api.mailchimp.com/3.0/lists/${process.env.NEXT_PUBLIC_MAILCHIMP_LIST_ID}/members`;
const { data } = await axios.post(url, providedData, {
headers: {
Authorization: `auth ${process.env.NEXT_PUBLIC_MAILCHIMP_API_KEY}`,
},
});
但是遇到了同样的问题!
我通过从 nextJS 服务器端
调用 Mailchimp API 解决了这个问题
你所要做的就是去 pages/api
并添加一条路由,代码如下(根据需要):
import mailchimp from '@mailchimp/mailchimp_marketing';
async function handler(req, res) {
const {email_address, status, merge_fields} = req.body
mailchimp.setConfig({
apiKey: process.env.NEXT_PUBLIC_MAILCHIMP_API_KEY,
server: process.env.NEXT_PUBLIC_MAILCHIMP_DC,
});
try {
await mailchimp.lists.addListMember(
process.env.NEXT_PUBLIC_MAILCHIMP_LIST_ID,
{
email_address,
status,
merge_fields
}
);
} catch (err) {
return res.status(400).send({ error: true })
}
return res.json({ success: true });
}
然后转到您的客户端并像这样获取此 API:
const res = await fetch('/api/signup', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email_address: email,
status: 'subscribed',
merge_fields: {
FNAME: name,
},
}),
});
就是这样
如何使用 mailchimp 处理 CORS 错误 API?
或者这段代码有什么问题:
这是使用 mailchimp 包:
import mailchimp from '@mailchimp/mailchimp_marketing'
mailchimp.setConfig({
apiKey: process.env.NEXT_PUBLIC_MAILCHIMP_API_KEY,
server: process.env.NEXT_PUBLIC_MAILCHIMP_DC,
});
try {
const response = await mailchimp.lists.addListMember(process.env.NEXT_PUBLIC_MAILCHIMP_LIST_ID, {
email_address: email,
status: "pending",
});
catch(err){}
这是使用 axios
const url = `https://${process.env.NEXT_PUBLIC_MAILCHIMP_DC}.api.mailchimp.com/3.0/lists/${process.env.NEXT_PUBLIC_MAILCHIMP_LIST_ID}/members`;
const { data } = await axios.post(url, providedData, {
headers: {
Authorization: `auth ${process.env.NEXT_PUBLIC_MAILCHIMP_API_KEY}`,
},
});
但是遇到了同样的问题!
我通过从 nextJS 服务器端
调用 Mailchimp API 解决了这个问题
你所要做的就是去 pages/api
并添加一条路由,代码如下(根据需要):
import mailchimp from '@mailchimp/mailchimp_marketing';
async function handler(req, res) {
const {email_address, status, merge_fields} = req.body
mailchimp.setConfig({
apiKey: process.env.NEXT_PUBLIC_MAILCHIMP_API_KEY,
server: process.env.NEXT_PUBLIC_MAILCHIMP_DC,
});
try {
await mailchimp.lists.addListMember(
process.env.NEXT_PUBLIC_MAILCHIMP_LIST_ID,
{
email_address,
status,
merge_fields
}
);
} catch (err) {
return res.status(400).send({ error: true })
}
return res.json({ success: true });
}
然后转到您的客户端并像这样获取此 API:
const res = await fetch('/api/signup', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email_address: email,
status: 'subscribed',
merge_fields: {
FNAME: name,
},
}),
});
就是这样