-1

How to perform a sync ajax call without using jquery?

Clarification: because there is a need to load a remote JSON resource before any of remote js scripts are executed, and then continue page loading.

setec
  • 13,254
  • 3
  • 30
  • 49
  • Does [this tutorial](http://code.tutsplus.com/articles/how-to-make-ajax-requests-with-raw-javascript--net-4855) help you? – René Roth Apr 30 '14 at 13:21
  • 1
    @RenéRoth question is about *sync*, not *async* call. – setec Apr 30 '14 at 13:24
  • This question has been answered, I believe, here: [STACKOVERFLOW](http://stackoverflow.com/questions/8567114/how-to-make-an-ajax-call-without-jquery) and here is how to make in synchronous [MOZILLA](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests) – JasonWilczak Apr 30 '14 at 13:25
  • Short answer: yes. Long answer: [read the docs](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest). Especially [here](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#open()) - note that `open` has an `async` parameter. – Matt Burland Apr 30 '14 at 13:26
  • 1
    A better answer might be to examine why you need to do this and if there isn't a better way to handle it. Synchronous calls will degrade the user experience. Nobody likes their browser to freeze. – Matt Burland Apr 30 '14 at 13:29

1 Answers1

2

Yes you can:

var request = new XMLHttpRequest();
request.open('GET', '/bar/foo.txt', false);  // `false` makes the request synchronous
request.send(null);

if (request.status === 200) {
  console.log(request.responseText);
}


This question has been answered, I believe, here on how to do a non-ajax call: STACKOVERFLOW and here is how to make in synchronous (where I got this answer from) MOZILLA

Community
  • 1
  • 1
JasonWilczak
  • 2,025
  • 1
  • 15
  • 35