Fetch usage and how to receive the value transmitted by JS. fetch receives the value transmitted by js.
Basic fetch method:
fetch('https://mywebsite.com/endpoint/', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json'}, body: JSON.stringify({ username: 'username', password: 'password'}) }).then(function(res){ console.log(res) })
Method 1: Add headers Definition
The headers are defined as follows:
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
At the same time, use the following method to pass the value to the body:
body:'username='+uname+'&password='+password
Use the following in php to receive
input('username')
Method 2: Change the Acceptance Method in php
The acceptance method is as follows:
$arr = file_get_contents("php://input");
Returns a String object. The value must be processed as follows:
$result=array(); foreach (explode('&', $arr) as $t){ list($a,$b)=explode('=', $t); $result[$a]=$b; }
In this case, the following values can be received:
$result['username']
Summary
The above section describes how to use fetch and how to receive JS values. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner!