Answers for "how to make a post request in js"

9

javascript send post request

let data = {element: "barium"};

fetch("/post/data/here", {
  method: "POST", 
  body: JSON.stringify(data)
}).then(res => {
  console.log("Request complete! response:", res);
});


// If you are as lazy as me (or just prefer a shortcut/helper):

window.post = function(url, data) {
  return fetch(url, {method: "POST", body: JSON.stringify(data)});
}

// ...

post("post/data/here", {element: "osmium"});
Posted by: Guest on March-04-2020
2

create http request javascript

const Http = new XMLHttpRequest();
const url='https://jsonplaceholder.typicode.com/posts';
Http.open("GET", url);
Http.send();

Http.onreadystatechange = (e) => {
  console.log(Http.responseText)
}
Posted by: Guest on September-14-2020
2

post method javascript code

const postData = async ( url = '', data = {})=>{
    console.log(data);
      const response = await fetch(url, {
      method: 'POST', 
      credentials: 'same-origin',
      headers: {
          'Content-Type': 'application/json',
      },
     // Body data type must match "Content-Type" header        
      body: JSON.stringify(data), 
    });

      try {
        const newData = await response.json();
        console.log(newData);
        return newData;
      }catch(error) {
      console.log("error", error);
      }
  }
Posted by: Guest on May-10-2020
0

how to make post request in javascript for api call

const data = {
    'keyone': 'valueone',
    'keytwo': 'valuetwo'
};

$.post(url, data, function(data,status){
	console.log(data);
});
Posted by: Guest on June-30-2021

Code answers related to "how to make a post request in js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language