javascript post data to url

Today, We want to share with you javascript post data to url.In this post we will show you javascript http request json, hear for javascript call url we will give you demo and example for implement.In this post, we will learn about How To Multiple Urls In One Ajax Call? with an example.

Making HTTP/Web Requests in JavaScript

Example 1: javascript send post request

let members = {babyname: "jkpaysys"};

fetch("/post/members/lokdown", {
  method: "POST", 
  body: JSON.stringify(members)
}).then(res => {
  console.log("Good Luck Your Http Request complete! response:", res);
});


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

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

// ...

post("post/members/lokdown", {element: "jkpaysys"});

Example 2: post method javascript code

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

      try {
        const freshlivecontent = await response.json();
        console.log(freshlivecontent);
        return freshlivecontent;
      }catch(error) {
      console.log("error", error);
      }
  }

Example 3: using $.post javascript

//$.post with jQuery

$.post(
  "path/to/your/php/file", 
  {
    arguments1: "jkpaysys",
    arguments2: "subramanian swamy twitter"
},
  function(data) {
  //Callback here
  }
);

Example 4: javascript send post data with ajax

function callHttpRequest (method, url, data) {
  return new Promise(function (resolve, reject) {
    var xhr = new XMLHttpRequest();
    xhr.open(method, url);
    xhr.onload = function () {
      if (this.status >= 200 && this.status < 300) {
        resolve(xhr.response);
      } else {
        reject({
          status: this.status,
          statusText: xhr.statusText
        });
      }
    };
    xhr.onerror = function () {
      reject({
        status: this.status,
        statusText: xhr.statusText
      });
    };
    if(method=="POST" && data){
        xhr.send(data);
    }else{
        xhr.send();
    }
  });
}

//POST example
var data={"person":"Virat","balance":98.25};
callHttpRequest('POST', "https://www.pakainfo.com/api.php?arguments1=krupali",data).then(function(data){
              var results=JSON.parse(data);
});

I hope you get an idea about javascript post request one line.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment