简体   繁体   English

如何制作一个异步/等待获取帖子? 得到500内部服务器错误

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

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(). 我已经成功地使用thisWorks()函数向我的php api发送了fetch post,但是我尝试将async / await post()函数与fetch一起使用,但是在尝试执行此操作时,我不断返回500内部服务器错误响应我不确定如何调试,因为异步函数中的代码与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 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;
    }
}

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 - 在此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$

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. 这个问题不是我的php代码中的问题,而是与我的提取请求有关,我通过向请求中添加新的Headers()来解决了该问题,并且它可以正常工作。 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. 因此,我确定问题在于我如何调用异步post()函数,但我不知道要更改什么。

EDIT- 编辑-

These are my headers in create.php. 这些是我在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 他们接受内容类型-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');

This was fixed by removing 'Accept' : 'application/json', 'Content-Type': 'application/json' from the new Headers. 通过从新标题中删除“ Accept”:“ application / json”,“ Content-Type”:“ application / json”来解决此问题。 That is what I'm accepting in my php headers but somehow it causes a 500 internal server error when used in a fetch post. 这就是我在php标头中接受的内容,但是以某种方式在获取帖子中使用时会导致500内部服务器错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM