3 examples of 'synchronous ajax' in JavaScript

Every line of 'synchronous ajax' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
15function doAjax(method, url, callback) {
16 if (window.GM_xmlhttpRequest) {
17 GM_xmlhttpRequest({
18 method: method,
19 url: url,
20 onload: callback,
21 });
22 } else {
23 var xhr = new XMLHttpRequest();
24 xhr.onreadystatechange = function() {
25 if (xhr.readyState !== 4 || xhr.status !== 200) return;
26 callback(xhr);
27 };
28 xhr.open(method, url, true);
29 }
30}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

60function ajax(options, callback) {
61 var origin = window.location.protocol + "//" + window.location.hostname + ":" + exports.PORT;
62 var request = $.ajax({
63 type: "GET",
64 url: origin + options.endpoint,
65 data: {
66 data: JSON.stringify(options.data),
67 socketId: options.connection.getSocketId()
68 },
69 async: options.async,
70 cache: false
71 });
72
73 if (options.async) {
74 request.done(function() {
75 if (request.status !== 200) throw new Error("Invalid status: " + request.status);
76 return callback(null, request.responseText);
77 });
78 request.fail(function(_, errorText) {
79 if (request.status !== 200) throw new Error("Invalid status: " + request.status);
80 throw new Error(errorText);
81 });
82 }
83 else {
84 if (request.status !== 200) throw new Error("Invalid status: " + request.status);
85 else return request.responseText;
86 }
87}
2function createAjax() {
3 let httpRequest;
4 if (window.XMLHTTPRequest) {
5 httpRequest = new XMLHttpRequest();
6 }else if(window.ActiveXObject){
7 httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
8 }
9 return httpRequest;
10}

Related snippets