POST 运行服务器 api 并响应客户端 500(内部服务器错误)
POST sails server api and react client 500 (Internal Server Error)
我使用 sails 服务器 api 并且我 post 来自 axios 反应但错误 500(内部服务器错误)。
这里代码客户端
export default function callApi2(endpoint, method = 'GET', body) {
let headers = { 'content-type': 'application/x-www-form-urlencoded' }
console.log(body);
return axios({
method: method,
url: `${Config.API_URL2}/${endpoint}`,
data: JSON.stringify(body),
headers: { 'content-type': 'application/x-www-form-urlencoded' },
}).catch(err => {
console.log(err);
});
此处代码服务器
postCommunication: async (req, res)=>{
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Allow-Methods','GET, POST, PATCH, PUT, DELETE, OPTIONS');
let newCommunication = req.body;
console.log(newCommunication);
console.log(JSON.stringify(newCommunication));
let resulfCommunication = await Communication.create(JSON.stringify(newCommunication)).fetch();
return res.json(resulfCommunication);
},
HTTP 状态为 500 表示服务器端出现故障。发生这种情况是因为您在 Axios 代码中传递了错误的内容类型,应该是 "application/json"
export default function callApi2(endpoint, method = 'GET', body) {
let headers = { 'content-type': 'application/json' }
console.log(body);
return axios({
method: method,
url: `${Config.API_URL2}/${endpoint}`,
data: JSON.stringify(body),
headers: { 'content-type': 'application/json' },
}).catch(err => {
console.log(err);
});
既然你提到 CORS headers 也不是必需的,你也可以删除下面的代码:
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Allow-Methods','GET, POST, PATCH, PUT, DELETE, OPTIONS');
欢迎来到 Whosebug,如果您觉得此答案有帮助,您可以将其标记为答案,以便它可以帮助遇到与您相同问题的人。
我使用 sails 服务器 api 并且我 post 来自 axios 反应但错误 500(内部服务器错误)。 这里代码客户端
export default function callApi2(endpoint, method = 'GET', body) {
let headers = { 'content-type': 'application/x-www-form-urlencoded' }
console.log(body);
return axios({
method: method,
url: `${Config.API_URL2}/${endpoint}`,
data: JSON.stringify(body),
headers: { 'content-type': 'application/x-www-form-urlencoded' },
}).catch(err => {
console.log(err);
});
此处代码服务器
postCommunication: async (req, res)=>{
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Allow-Methods','GET, POST, PATCH, PUT, DELETE, OPTIONS');
let newCommunication = req.body;
console.log(newCommunication);
console.log(JSON.stringify(newCommunication));
let resulfCommunication = await Communication.create(JSON.stringify(newCommunication)).fetch();
return res.json(resulfCommunication);
},
HTTP 状态为 500 表示服务器端出现故障。发生这种情况是因为您在 Axios 代码中传递了错误的内容类型,应该是 "application/json"
export default function callApi2(endpoint, method = 'GET', body) {
let headers = { 'content-type': 'application/json' }
console.log(body);
return axios({
method: method,
url: `${Config.API_URL2}/${endpoint}`,
data: JSON.stringify(body),
headers: { 'content-type': 'application/json' },
}).catch(err => {
console.log(err);
});
既然你提到 CORS headers 也不是必需的,你也可以删除下面的代码:
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Allow-Methods','GET, POST, PATCH, PUT, DELETE, OPTIONS');
欢迎来到 Whosebug,如果您觉得此答案有帮助,您可以将其标记为答案,以便它可以帮助遇到与您相同问题的人。