1let data = {element: "barium"};
2
3fetch("/post/data/here", {
4 method: "POST",
5 body: JSON.stringify(data)
6}).then(res => {
7 console.log("Request complete! response:", res);
8});
9
10
11// If you are as lazy as me (or just prefer a shortcut/helper):
12
13window.post = function(url, data) {
14 return fetch(url, {method: "POST", body: JSON.stringify(data)});
15}
16
17// ...
18
19post("post/data/here", {element: "osmium"});
20
1var xhr = new XMLHttpRequest();
2xhr.open("POST", yourUrl, true);
3xhr.setRequestHeader('Content-Type', 'application/json');
4xhr.send(JSON.stringify({
5 value: value
6}));
1function makeRequest (method, url, data) {
2 return new Promise(function (resolve, reject) {
3 var xhr = new XMLHttpRequest();
4 xhr.open(method, url);
5 xhr.onload = function () {
6 if (this.status >= 200 && this.status < 300) {
7 resolve(xhr.response);
8 } else {
9 reject({
10 status: this.status,
11 statusText: xhr.statusText
12 });
13 }
14 };
15 xhr.onerror = function () {
16 reject({
17 status: this.status,
18 statusText: xhr.statusText
19 });
20 };
21 if(method=="POST" && data){
22 xhr.send(data);
23 }else{
24 xhr.send();
25 }
26 });
27}
28
29//POST example
30var data={"person":"john","balance":1.23};
31makeRequest('POST', "https://www.codegrepper.com/endpoint.php?param1=yoyoma",data).then(function(data){
32 var results=JSON.parse(data);
33});
1function httpGetAsync(url, callback) {
2 var xmlHttp = new XMLHttpRequest();
3 xmlHttp.onreadystatechange = function() {
4 if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
5 callback(xmlHttp.responseText);
6 }
7 xmlHttp.open("GET", url, true); // true for asynchronous
8 xmlHttp.send(null);
9}
1const postData = async ( url = '', data = {})=>{
2 console.log(data);
3 const response = await fetch(url, {
4 method: 'POST',
5 credentials: 'same-origin',
6 headers: {
7 'Content-Type': 'application/json',
8 },
9 // Body data type must match "Content-Type" header
10 body: JSON.stringify(data),
11 });
12
13 try {
14 const newData = await response.json();
15 console.log(newData);
16 return newData;
17 }catch(error) {
18 console.log("error", error);
19 }
20 }
21
1<!DOCTYPE html>
2<html>
3 <head>
4 <meta charset="UTF-8">
5 <meta http-equiv="X-UA-Compatible" content="IE=edge">
6 <meta name="viewport" content="width=device-width, initial-scale=1.0">
7 <link rel="stylesheet" href="assets/style.css">
8 <!-- <script src="assets/javascript.js"></script> -->
9 <title>Telegramga habar jo`natish</title>
10 </head>
11 <body>
12 <div class="contaakb">
13 <h2>Telegramga habar yozish</h2>
14 <p>Guruhlarga habar yozish uchun qilingan modul</p>
15 </div>
16 <div class="container">
17 <form action="?" method="post">
18 <div class="row">
19 <div class="col-25">
20 <label for="fname">Guruh nomi</label>
21 </div>
22 <div class="col-75">
23 <input type="text" id="group" name="group" placeholder="Telegram guruhning nomini yozing" value="" autofocus>
24 </div>
25 </div>
26 <div class="row">
27 <div class="col-25">
28 <label for="country">Guruh (ixtiyoriy)</label>
29 </div>
30 <div class="col-75">
31 <select id="select" name="select" onchange="getComboA(this)">
32 <option selected value="tanlang">Tanlang</option>
33 <option value="ITspeciallessons2">Qobiljon</option>
34 <option value="ITspeciallessons1">Azizbek</option>
35 <option value="taxiuchqorgontoshkent">Uchqorgon Toshkent TAXI</option>
36 <option value="clashuzhackersw">ClashUzHackerSW</option>
37 <option value="nodirjonbotirov">Nodirjon ISh</option>
38 </select>
39 </div>
40 </div>
41 <div class="row">
42 <div class="col-25">
43 <label for="subject">Habar matni</label>
44 </div>
45 <div class="col-75">
46 <textarea id="message" name="message" placeholder="Habar matnini yozing" style="height:200px" autofocus></textarea>
47 </div>
48 </div>
49 <div class="row">
50 <button type="submit" onclick="loadDoc(event)">
51 Jo'natish
52 </button>
53 </div>
54 </form>
55 </div>
56 <script type="text/javascript">
57 function getComboA(selectObject) {
58 document.getElementById("group").disabled = true;
59 document.getElementById("message").focus();
60 console.log('group disabled');
61 }
62
63 function loadDoc(event) {
64 document.getElementById("message").disabled = true;
65 document.getElementById("select").disabled = true;
66 document.getElementById("group").disabled = true;
67
68 var params = 'message='+document.getElementById('message').value+'&select='+document.getElementById('select').value+'&group='+document.getElementById('group').value+'&ajax=yes';
69 var xhttp = new XMLHttpRequest();
70 xhttp.open("POST", "?", true);
71 xhttp.onreadystatechange = function() {
72 if (this.readyState == 4 && this.status == 200) {
73 document.getElementById("demo").innerHTML = this.responseText;
74 }
75 };
76 xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
77 xhttp.onreadystatechange = function() {//Call a function when the state changes.
78 if(xhttp.readyState == 4 && xhttp.status == 200) {
79 alert(xhttp.responseText);
80 document.getElementById("message").disabled = false;
81 document.getElementById("select").disabled = false;
82 document.getElementById("group").disabled = false;
83 }
84 }
85 xhttp.send(params);
86 event.preventDefault();
87 }
88 </script>
89 </body>
90</html>
91