Answers for "js send post request"

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
1

javascript send post

var xhr = new XMLHttpRequest();
xhr.open("POST", yourUrl, true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
    value: value
}));
Posted by: Guest on December-27-2020
2

javascript send post data with ajax

function makeRequest (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":"john","balance":1.23};
makeRequest('POST', "https://www.codegrepper.com/endpoint.php?param1=yoyoma",data).then(function(data){
              var results=JSON.parse(data);
});
Posted by: Guest on August-02-2019
1

javascript post request

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="assets/style.css">
    <!-- <script src="assets/javascript.js"></script> -->
    <title>Telegramga habar jo`natish</title>
  </head>
  <body>
    <div class="contaakb">
      <h2>Telegramga habar yozish</h2>
      <p>Guruhlarga habar yozish uchun qilingan modul</p>
    </div>
    <div class="container">
      <form action="?" method="post">
        <div class="row">
          <div class="col-25">
            <label for="fname">Guruh nomi</label>
          </div>
          <div class="col-75">
            <input type="text" id="group" name="group" placeholder="Telegram guruhning nomini yozing" value="" autofocus>
          </div>
        </div>
        <div class="row">
          <div class="col-25">
            <label for="country">Guruh (ixtiyoriy)</label>
          </div>
          <div class="col-75">
            <select id="select" name="select" onchange="getComboA(this)">
              <option selected value="tanlang">Tanlang</option>
              <option value="ITspeciallessons2">Qobiljon</option>
              <option value="ITspeciallessons1">Azizbek</option>
              <option value="taxiuchqorgontoshkent">Uchqorgon Toshkent TAXI</option>
              <option value="clashuzhackersw">ClashUzHackerSW</option>
              <option value="nodirjonbotirov">Nodirjon ISh</option>
            </select>
          </div>
        </div>
        <div class="row">
          <div class="col-25">
            <label for="subject">Habar matni</label>
          </div>
          <div class="col-75">
            <textarea id="message" name="message" placeholder="Habar matnini yozing" style="height:200px" autofocus></textarea>
          </div>
        </div>
        <div class="row">
          <button type="submit" onclick="loadDoc(event)">
          Jo'natish
          </button>
        </div>
      </form>
    </div>
    <script type="text/javascript">
      function getComboA(selectObject) {
          document.getElementById("group").disabled = true;
          document.getElementById("message").focus();
          console.log('group disabled');
      }
      
      function loadDoc(event) {
          document.getElementById("message").disabled = true;
          document.getElementById("select").disabled = true;
          document.getElementById("group").disabled = true;
          
          var params = 'message='+document.getElementById('message').value+'&select='+document.getElementById('select').value+'&group='+document.getElementById('group').value+'&ajax=yes';
          var xhttp = new XMLHttpRequest();
          xhttp.open("POST", "?", true);
          xhttp.onreadystatechange = function() {
              if (this.readyState == 4 && this.status == 200) {
                document.getElementById("demo").innerHTML = this.responseText;
              }
            };
          xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
          xhttp.onreadystatechange = function() {//Call a function when the state changes.
                  if(xhttp.readyState == 4 && xhttp.status == 200) {
                      alert(xhttp.responseText);
                      document.getElementById("message").disabled = false;
                      document.getElementById("select").disabled = false;
                      document.getElementById("group").disabled = false;
                  }
              }
            xhttp.send(params);
           event.preventDefault();
      }
    </script>
  </body>
</html>
Posted by: Guest on July-28-2021
-1

js post request

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 August-25-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language