JS

POST & GET WITHOUT AN HTML FORM IN JAVASCRIPT

(quick guide & examples)

DATA var data = new URLSearchParams(); data.append("name", "Jon Doe");

REDIRECT var url = "http://site.com/script?" + data.toString(); location.href = url;

URL QUERY STRING

01

DATA var data = new FormData();  data.append("name", "Jon Doe"); 

AJAX POST var xhr = new XMLHttpRequest(); xhr.open("POST", "http://site.com"); xhr.onload = function () { console.log(this.response); };  xhr.send(data);

AJAX POST

02

DATA var data = new URLSearchParams(); data.append("name", "Joh Doe");  var url = "http://site.com/script?" + data.toString();

AJAX GET var xhr = new XMLHttpRequest(); xhr.open("GET", url); xhr.onload = function() { console.log(this.response); };  xhr.send();

AJAX GET

03

DATA var data = new URLSearchParams(); data.append("name", "Jon Doe"); 

FETCH POST fetch("http://mysite.com/SCRIPT", { method: "post", body: data }) .then(res => res.text()) .then(txt => console.log(txt)) .catch(err => console.log(error));

FETCH POST

04

DATA var data = new URLSearchParams(); data.append("name", "Jon Doe"); 

FETCH GET fetch("http://mysite.com/SCRIPT?" + data.toString(), { method: "get" }) .then(res => res.text()) .then(txt => console.log(txt)) .catch(err => console.log(error));

FETCH GET

05