How to grab data using fetch() API POST method in PHP?

后端 未结 3 1548
野的像风
野的像风 2021-02-02 00:42

I am trying to use fetch() API POST method in order to grab the POST data in PHP.

Here is what I have tried:

var x = \"hello\";
fetch(url,{m         


        
3条回答
  •  时光取名叫无心
    2021-02-02 01:00

    Appearently, when using the Fetch API to send data to a PHP server, you'll have to handle the request a little different from what you're used to.

    The data you're "POSTing" or "GETting" is not going to be available in the super global variables since this input is not coming from a multipart-data form or an application/x-www-form-urlencoded

    You can get your data by reading the special file: php://input, for example using file_get_contents('php://input') and then try to decode that input with json_decode().

    Hopefully it helps.

    You cand read more about it here:

    https://codepen.io/dericksozo/post/fetch-api-json-php

提交回复
热议问题