[英]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.