[英]How to make an Async/await fetch post? getting 500 internal server error
我已经成功地使用thisWorks()函数向我的php api发送了fetch post,但是我尝试将async / await post()函数与fetch一起使用,但是在尝试执行此操作时,我不断返回500内部服务器错误响应我不确定如何调试,因为异步函数中的代码与thisWorks()中的代码基本相同。
这是两个功能-
//I can make a succesful post to my api with this function and it returns a 200 ok response from the server
function thisWorks(title, message, author, id) {
fetch("http://menu.com/menu/api/post/create.php", {
method: "POST",
headers: new Headers(),
body: JSON.stringify({
'title' : title,
'body' : message,
'author' : author,
'category_id' : id
})
})
.then( (response) => {
console.log(response);
});
}
//this gives a 500 internal server error and TypeError: "NetworkError when attempting to fetch resource."
const post = async (title, message, author, id) => {
const location = "http://menu.com/menu/api/post/create.php";
const settings = {
method: 'POST',
headers: new Headers({
'Accept' : 'application/json',
'Content-Type': 'application/json'
}),
body: JSON.stringify({
'title' : title,
'body' : message,
'author' : author,
'category_id' : id
})
};
try {
const response = await fetch(location, settings);
const json = await response.json();
console.log(json);
return json;
} catch (error) {
console.log(error);
return error;
}
}
在此Works中出现相同的500个内部服务器错误之前,我的apache服务器中的errorLog是我现在在异步发布函数中遇到的相同错误-
[Sun Feb 17 18:21:11.122109 2019] [php7:warn] [pid 890] [client 127.0.0.1:37548] PHP Warning: Header may not contain more than a single header, new line detected in /home/orpheus/Practice_dev/menu/api/post/create.php on line 7
[Sun Feb 17 18:21:11.122452 2019] [php7:notice] [pid 890] [client 127.0.0.1:37548] PHP Notice: Trying to get property 'title' of non-object in /home/orpheus/Practice_dev/menu/api/post/create.php on line 22
[Sun Feb 17 18:21:11.122471 2019] [php7:notice] [pid 890] [client 127.0.0.1:37548] PHP Notice: Trying to get property 'body' of non-object in /home/orpheus/Practice_dev/menu/api/post/create.php on line 23
[Sun Feb 17 18:21:11.122477 2019] [php7:notice] [pid 890] [client 127.0.0.1:37548] PHP Notice: Trying to get property 'author' of non-object in /home/orpheus/Practice_dev/menu/api/post/create.php on line 24
[Sun Feb 17 18:21:11.122482 2019] [php7:notice] [pid 890] [client 127.0.0.1:37548] PHP Notice: Trying to get property 'category_id' of non-object in /home/orpheus/Practice_dev/menu/api/post/create.php on line 25
[Sun Feb 17 18:21:11.122743 2019] [php7:error] [pid 890] [client 127.0.0.1:37548] PHP Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'category_id' at row 1 in /home/orpheu$
这个问题不是我的php代码中的问题,而是与我的提取请求有关,我通过向请求中添加新的Headers()来解决了该问题,并且它可以正常工作。 因此,我确定问题在于我如何调用异步post()函数,但我不知道要更改什么。
编辑-
这些是我在create.php中的头文件。 他们接受内容类型-application / json,所以我不确定为什么不能在没有错误的情况下将其添加到我的ajax请求中
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
header('Access-Control-Allow-Methods: POST, OPTIONS');
header("Access-Control-Max-Age: 3600");
header('Access-Control-Allow-Headers: Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods,
Authorization, X-Requested-With, Content-Type, Authorization');
通过从新标题中删除“ Accept”:“ application / json”,“ Content-Type”:“ application / json”来解决此问题。 这就是我在php标头中接受的内容,但是以某种方式在获取帖子中使用时会导致500内部服务器错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.