Javascript Run Same Ajax Function Again Depending on Response

6 Dissimilar ways to exercise Ajax calls in JavaScript

Ajax Interview Questions

1. XHR

          const Http = new XMLHttpRequest();
const url='http://yourdomain.com/';
Http.open("GET", url);
Http.send();
Http.onreadystatechange=(e)=>{
console.log(Http.responseText)
}
          var xhr = new XMLHttpRequest();
xhr.open up("Mail", '/submit', truthful);
xhr.setRequestHeader("Content-Type", "application/x-world wide web-form-urlencoded");
xhr.onreadystatechange = function() {
if (this.readyState === XMLHttpRequest.Washed && this.status === 200) {
// Request finished. Practise processing here.
}
}
xhr.send("name=Ketan&id=i");

2. Fetch API

          fetch('https://www.yourdomain.com', {
method: 'get'
})
.then(response => response.json())
.so(jsonData => console.log(jsonData))
.catch(err => {
//fault block
}
          var url = 'https://www.yourdomain.com/updateProfile';
var data = {username: 'courseya'};
fetch(url, {
method: 'POST', // or 'PUT'
body: JSON.stringify(data), // information tin be `string` or {object}!
headers:{
'Content-Type': 'application/json'
}
}).then(res => res.json())
.so(response => console.log('Success:', JSON.stringify(response)))
.grab(fault => panel.error('Mistake:', error));

3. jQuery

Jquery Interview Questions
          $.ajax({
url: '/users',
type: "Become",
dataType: "json",
success: function (data) {
console.log(data);
},
error: function (error) {
panel.log(`Error ${error}`);
}
});
          $.ajax({
url: '/users',
blazon: "Mail",
data: {
name: "Ipseeta",
id: 1
},
dataType: "json",
success: office (data) {
console.log(information);
},
error: function (error) {
console.log(`Error ${error}`);
}
});

iv. Axios

          axios.get('/become-user', {
params: {
ID: i
}
})
.so(function (response) {
console.log(response);
})
.catch(office (mistake) {
panel.log(error);
})
.then(function () {
// ever executed
});
          axios.post('/user', {
name: 'Sanjeev',
id: 1
})
.and so(part (response) {
console.log(response);
})
.grab(office (error) {
console.log(error);
});

five. Request

          var request = require('request');
request('http://www.yourdomain.com', function (fault, response, body) {
panel.log('error:', error);
console.log('statusCode:', response && response.statusCode);
panel.log('torso:', torso);
});

six. SuperAgent

          request
.get('/user')
.query({ id: ane })
.then(res => {
});
          asking.post('/user')
.set('Content-Type', 'application/json')
.send('{"proper noun":"Ipseeta","id":1}')
.then(callback)
.catch(errorCallback)

Conclusion

elliotyoute1944.blogspot.com

Source: https://medium.com/@Sharad35386442/6-different-ways-to-do-ajax-calls-in-javascript-b47200fe7a38

0 Response to "Javascript Run Same Ajax Function Again Depending on Response"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel