javascript ajax request
JavaScript
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();
}
});
}
//GET example
makeRequest('GET', "https://www.codegrepper.com/endpoint.php?param1=yoyoma").then(function(data){
var results=JSON.parse(data);
});
//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);
});<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
//looking for a change or state , like a request or get.
xhttp.onreadystatechange = function() {
//if equal to 4 means that its ready.
// if equal to 200 indicates that the request has succeeded.
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
//GET method for gettin the data // the file you are requesting
xhttp.open("GET", "TheFileYouWant.html", true);
//sending the request
xhttp.send();
}//Change the text of a <div> element using an AJAX //request:
//using JQuery
$("button").click(function(){
$.ajax({url: "demo_test.txt", success: function(result){
$("#div1").html(result);
}});
});
//To send a request to a server, we use the open() //and send() methods of the XMLHttpRequest object:
// Javascript
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
//example below
<html>
<body>
<h1>The XMLHttpRequest Object</h1>
<button type="button" onclick="loadDoc()">Request data</button>
<p id="demo"></p>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("GET", "demo_get.asp", true);
xhttp.send();
}
</script>
</body>
</html>
Also in JavaScript:
- Title
- format to currency javascript
- Category
- JavaScript
- Title
- javascript check if array is in array
- Category
- JavaScript
- Title
- how to add class to element on mouseclick with js
- Category
- JavaScript
- Title
- free map js
- Category
- JavaScript
- Title
- how to get today date in javascript
- Category
- JavaScript
- Title
- change id in jquery
- Category
- JavaScript
- Title
- dropdown list value react fragment
- Category
- JavaScript
- Title
- d3 paning
- Category
- JavaScript
- Title
- get all keys in json object
- Category
- JavaScript
- Title
- cant find variable react
- Category
- JavaScript
- Title
- create a customer in stripe node.js
- Category
- JavaScript
- Title
- adonisjs vscode debugger
- Category
- JavaScript
- Title
- how to change h1 text using javascript from node
- Category
- JavaScript
- Title
- access session data from ejs view
- Category
- JavaScript
- Title
- get input value in angular 7
- Category
- JavaScript
- Title
- if checkbox checked jquery value 1
- Category
- JavaScript
- Title
- how to make one line if in js
- Category
- JavaScript
- Title
- how to convert time to am pm in javascript
- Category
- JavaScript
- Title
- deploy angular app on google app engine
- Category
- JavaScript
- Title
- function js
- Category
- JavaScript
- Title
- how to do bubble sort in javascript
- Category
- JavaScript
- Title
- how to see if a web site is useing react
- Category
- JavaScript
- Title
- get value of hidden field jquery
- Category
- JavaScript
- Title
- callback without duplicates javascript
- Category
- JavaScript
- Title
- How do I check whether a checkbox is checked in jQuery?
- Category
- JavaScript
- Title
- funcion dentro de objeto
- Category
- JavaScript
- Title
- how to check chrome version in js
- Category
- JavaScript
- Title
- how to call action from another module vuex
- Category
- JavaScript
- Title
- how to set diferent images in html through js
- Category
- JavaScript
- Title
- Cannot find module '.json'
- Category
- JavaScript
- Title
- how to add multiple videos in html5 with javascript
- Category
- JavaScript
- Title
- angular size of array
- Category
- JavaScript
- Title
- for javascript delay
- Category
- JavaScript
- Title
- get current directory vbscript
- Category
- JavaScript
- Title
- how to create a variable in javascript
- Category
- JavaScript
- Title
- get the id of a div in jquery
- Category
- JavaScript
- Title
- body-parser node
- Category
- JavaScript
- Title
- Angular material design
- Category
- JavaScript
- Title
- create a react native project
- Category
- JavaScript
- Title
- how to validate input type=date in javascript
- Category
- JavaScript
- Title
- add a route to a buttoin in angular
- Category
- JavaScript
- Title
- coment executer des touche de clavier avec du js
- Category
- JavaScript
- Title
- chrome extension dynamically add javascript
- Category
- JavaScript
- Title
- bootstrap 4 hide modal via javascript
- Category
- JavaScript
- Title
- getelementbyid
- Category
- JavaScript
- Title
- graal.js javascript array in java
- Category
- JavaScript
- Title
- getFullYear within moment in angular
- Category
- JavaScript
- Title
- ajax with progress bar
- Category
- JavaScript
- Title
- array unique values javascript
- Category
- JavaScript
- Title
- how to get element by title js
- Category
- JavaScript
- Title
- javascript add option element to select
- Category
- JavaScript
- Title
- electron jquery
- Category
- JavaScript
- Title
- How to uninstall npm modules in node js?
- Category
- JavaScript
- Title
- array.filter in javascript not working in w3schools
- Category
- JavaScript
- Title
- how to remove an class in javascript
- Category
- JavaScript
- Title
- how to deploy react app in tomcat server
- Category
- JavaScript
- Title
- convert json to object jackson
- Category
- JavaScript
- Title
- iterate over object javascript
- Category
- JavaScript
- Title
- How can I upload files asynchronously
- Category
- JavaScript
- Title
- href before onclick js
- Category
- JavaScript
- Title
- button click function in js
- Category
- JavaScript
- Title
- first element of array js
- Category
- JavaScript
- Title
- how to Write a program that simulates a coin toss using random method of Javascript Math class
- Category
- JavaScript
- Title
- get url params angular
- Category
- JavaScript
- Title
- change input placeholder text jquery
- Category
- JavaScript
- Title
- express how to access ajax post data
- Category
- JavaScript
- Title
- create react app theme_color
- Category
- JavaScript
- Title
- alert user javascript
- Category
- JavaScript
- Title
- adding a variable to a string without using + in javascript
- Category
- JavaScript
- Title
- app script append two list
- Category
- JavaScript
- Title
- creating an object javascript
- Category
- JavaScript
- Title
- Javascript check if undefinded
- Category
- JavaScript
- Title
- how to start react
- Category
- JavaScript
- Title
- javascript after split then trim whitespace
- Category
- JavaScript
- Title
- bubble sort
- Category
- JavaScript
- Title
- axios file upload
- Category
- JavaScript
- Title
- iis express gzip
- Category
- JavaScript
- Title
- how to get data in json format in javascript
- Category
- JavaScript
- Title
- conditional rendering with radio buttons
- Category
- JavaScript
- Title
- app listen with all error handling
- Category
- JavaScript
- Title
- find the last occurrence of a character in a string javascript
- Category
- JavaScript
- Title
- hover jquery
- Category
- JavaScript
- Title
- add to json object javascript
- Category
- JavaScript
- Title
- concat js mdn
- Category
- JavaScript
- Title
- find highest number in array javascript
- Category
- JavaScript
- Title
- how to multiple loop in javascript using for
- Category
- JavaScript
- Title
- filter javascript array
- Category
- JavaScript
- Title
- eval in js
- Category
- JavaScript
- Title
- initialize a map js
- Category
- JavaScript
- Title
- generate random numbers in js
- Category
- JavaScript
- Title
- how to take input in javascript in coding
- Category
- JavaScript
- Title
- create new node project
- Category
- JavaScript
- Title
- how to create duplicate key array in javascript
- Category
- JavaScript
- Title
- get date now javascript
- Category
- JavaScript
- Title
- how to display image in html from json object
- Category
- JavaScript
- Title
- delete from array javascript
- Category
- JavaScript
- Title
- async reduce javascript
- Category
- JavaScript
- Title
- butonc click jquery
- Category
- JavaScript
- Title
- javascript check empty object
- Category
- JavaScript
- Title
- if alternative javascript
- Category
- JavaScript