Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 4085

How to make an Async/await fetch post? getting 500 internal server error

$
0
0

I have succesfuly made a fetch post to my php api with my thisWorks() function, but I am trying to use fetch with async/await post () function but I keep getting back a 500 internal server error response when trying to do this which I'm not sure how to debug since my code in my async function is mostly the same as in thisWorks().

here are the two functions -

//I can make a succesful post to my api with this function and it returns a 200 ok response from the serverfunction 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;    }}

Before I was getting the same 500 internal server error in thisWorks and the errorLog in my apache server was the same errors I'm now getting in my async post function which is this -

[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$

The issue was not one in my php code, but it was with my fetch request and I solved it by adding new Headers() to the request and it worked. So I'm sure the issue is in some error in how I'm calling the async post() function, but I don't know what to change.

EDIT-

These are my headers in create.php. They accept content type - application/json so I'm not sure why I can't add that to my fetch ajax requests without getting errors

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');

Viewing all articles
Browse latest Browse all 4085

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>