NextJS API 不发布到外部域
NextJS API not posting to external domains
我在 Vercel 中有一个简单的 NextJS 应用程序 运行。我克隆了 Vercel 提供的 NextJS 模板,并只添加了一个名为 jira.js
的文件
我只是想 post 随机数据到外部 API 当这个 jira
被击中时。
Jira.js以下
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
import axios from 'axios'
import https from 'https'
export default (req, res) => {
const headers = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
axios.post('https://webhook.site/6db7a14b-48d7-4037-a482-86885526aa40', {
Name: 'Fred',
Age: '23'
}, {
headers: headers,
withCredentials: true
}
).then(function(res) {
console.log({res: res})
}).catch(function(e) {
console.log({"failed": e})
})
res.json({ status: 'ok' })
}
当我在本地尝试它时 (localhost:3000/api/jira
),数据正在 posted 到 Webhook 站点,但是当我将它部署到 vercel(random-domain.com/api/jira
) 时,就会有webhook 站点中没有 posting 数据,但我在浏览器中收到 Status: ok 消息。
我对此很陌生? somoene 可以指导我解决我所缺少的问题吗?
您还没有将您的函数标记为 async
,所以我认为它不会等待 JIRA 返回的响应。例如:
export default async (req, res) => {
try {
const response = await fetch(
`https://webhook.site/6db7a14b-48d7-4037-a482-86885526aa40`,
{
body: JSON.stringify({
Name: 'Fred',
Age: '23'
}),
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
},
method: 'POST'
}
);
if (response.status >= 400) {
return res.status(400).json({
error: 'There was an error'
});
}
return res.status(200).json({ status: 'ok' });
} catch (error) {
return res.status(500).json({
error: 'There was an error'
});
}
};
您也不需要 axios
- fetch
默认为您填充。
我在 Vercel 中有一个简单的 NextJS 应用程序 运行。我克隆了 Vercel 提供的 NextJS 模板,并只添加了一个名为 jira.js
的文件我只是想 post 随机数据到外部 API 当这个 jira
被击中时。
Jira.js以下
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
import axios from 'axios'
import https from 'https'
export default (req, res) => {
const headers = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
axios.post('https://webhook.site/6db7a14b-48d7-4037-a482-86885526aa40', {
Name: 'Fred',
Age: '23'
}, {
headers: headers,
withCredentials: true
}
).then(function(res) {
console.log({res: res})
}).catch(function(e) {
console.log({"failed": e})
})
res.json({ status: 'ok' })
}
当我在本地尝试它时 (localhost:3000/api/jira
),数据正在 posted 到 Webhook 站点,但是当我将它部署到 vercel(random-domain.com/api/jira
) 时,就会有webhook 站点中没有 posting 数据,但我在浏览器中收到 Status: ok 消息。
我对此很陌生? somoene 可以指导我解决我所缺少的问题吗?
您还没有将您的函数标记为 async
,所以我认为它不会等待 JIRA 返回的响应。例如:
export default async (req, res) => {
try {
const response = await fetch(
`https://webhook.site/6db7a14b-48d7-4037-a482-86885526aa40`,
{
body: JSON.stringify({
Name: 'Fred',
Age: '23'
}),
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
},
method: 'POST'
}
);
if (response.status >= 400) {
return res.status(400).json({
error: 'There was an error'
});
}
return res.status(200).json({ status: 'ok' });
} catch (error) {
return res.status(500).json({
error: 'There was an error'
});
}
};
您也不需要 axios
- fetch
默认为您填充。