Answers for "post method with javascript"

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
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