-
Notifications
You must be signed in to change notification settings - Fork 0
Fetch
Vital-jan edited this page Dec 28, 2020
·
3 revisions
Method 1:
fetch('test2.php', {
method: 'post',
headers: {"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"},
body: 'foo=bar&lorem=ipsum' // similar GET request
})
Method 2:
let fd = new FormData;
fd.append('start', 1);
fd.append('selected', JSON.stringify([1,2,3,4,5]))
fetch('test2.php', {
method: "POST",
body: fd
})
.then((response)=>{
if (response.status == 200) { // successful ajax request
console.log('Succefull request to ' + response.url + ' // Response type = ' + response.type);
}
else {console.log('Error request to ' + response.url + ' //status ' + response.status + ' ' + response.statusText)} // unsuccessful ajax request
// to read the text, php returned:
return response.text();
// return response.json();
})
.then((resp)=> {
console.log(resp)
})
.catch((error)=>{
console.log('No data returned, possible error occurred in php file.')
});
PHP for method 2:
<? var_dump($_POST['selected']); $s = $_POST['selected']; var_dump(json_decode($s)); ?>