javascript post to url – How to Send POST Requests with JavaScript Fetch API?

0
()

javascript post to url – JavaScript has great modules and methods to make HTTP requests – post to url Example with demo Like javascript send post request, javascript send post data with ajax, post method javascript code and $.post javascript.

javascript post to url

JavaScript post request like a form submit Dynamically create HTML s in a form and submit it. Here are the most popular ways to make an HTTP request in JavaScript.

function postHttpCall(path, params, method='post') {
  const form = document.createElement('form');
  form.method = method;
  form.action = path;

  for (const key in params) {
    if (params.hasOwnProperty(key)) {
      const userSignInParam = document.createElement('input');
      userSignInParam.type = 'hidden';
      userSignInParam.name = key;
      userSignInParam.value = params[key];

      form.appendChild(userSignInParam);
    }
  }

  document.body.appendChild(form);
  form.submit();
}

Example

postHttpCall('/login/', {name: 'PakainfoV123'});

javascript send post request

let response = {login: "pakainfo_v1"};

fetch("/admin/call/http/Api.php", {
  method: "POST", 
  body: JSON.stringify(response)
}).then(res => {
  console.log("Great!!, Request complete! response:", res);
});

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

// ...

post("post/data/here", {element: "osmium"});

javascript send post data with ajax

function postHttpCall (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();
    }
  });
}

//Http POST example
var data={"member":"virat","password":45548V2154};
postHttpCall('POST', "https://www.domanin-name-admin.com/api.php?aitsmgm=yogibba",data).then(function(data){
              var results=JSON.parse(data);
});

post method javascript code

HTML Body data type must match “Content-Type” header

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

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

$.post javascript

Simple $.post with jQuery Example

$.post(
  "full-path/admin/api/v1/login", 
  {
    loginname: "PakainfoV12",
    password: "[email protected]"
},
  function(data) {
  //Callback here
  }
);

Don’t Miss : Javascript Post Data To Url

Download

I hope you get an idea about javascript post to url.
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.

Read Also:  Simple DateTime range with two inputs Example

How useful was this post?

Click on a star to rate it!

Free Live Chat for Any Issue