Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

How to fetch API data using POST and GET in PHP

// USING GET
//_____________________________________
//If you want to $_GET['x'], you need to send the data in the querystring:
var url = '/your/url?x=hello';

fetch(url)
.then(function (response) {
  return response.text();
})
.then(function (body) {
  console.log(body);
});
// USING POST
//___________________________________________
//If you want to $_POST['x'], you need to send the data as FormData:

var url = '/your/url';
var formData = new FormData();
formData.append('x', 'hello');

fetch(url, { method: 'POST', body: formData })
.then(function (response) {
  return response.text();
})
.then(function (body) {
  console.log(body);
});
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #How #fetch #API #data #POST #GET #PHP
ADD COMMENT
Topic
Name
4+4 =