create http request javascript
const Http = new XMLHttpRequest();
const url='https://jsonplaceholder.typicode.com/posts';
Http.open("GET", url);
Http.send();
Http.onreadystatechange = (e) => {
console.log(Http.responseText)
}
create http request javascript
const Http = new XMLHttpRequest();
const url='https://jsonplaceholder.typicode.com/posts';
Http.open("GET", url);
Http.send();
Http.onreadystatechange = (e) => {
console.log(Http.responseText)
}
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);
});
http request javascript
function httpGetAsync(url, callback) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", url, true); // true for asynchronous
xmlHttp.send(null);
}
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);
}
}
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>
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us