EXT Js Synchronous ajax request

44,761

Solution 1

I needed something similar and after looking the source code of Ext.Ajax.request() and Ext.data.Connection, I found that they check for a async attribute, on the request() method, so, it's actually possible to make a synchronous request, like this:

var response = Ext.Ajax.request({
    async: false,
    url: 'service/url'
});
var items = Ext.decode(response.responseText);

It seems this async attribute is not documented, so... of course... be aware that it may change in future releases.

Solution 2

In Extjs.Ajax each AJAX call results in one of three types of callback functions:

  • success that runs if the AJAX call is successfully created and gets a response
  • failure that runs if the AJAX call fails to get a response
  • callback that runs AFTER the AJAX call gets a response

If you want to execute test2(); after your ajax request finishes,
then put your test2 function inside of a success, failure, or callback function; whichever one you choose is up to you...

More info on the success or failure function can be found here.

test1();
Ext.Ajax.request({
    url: 'page.php',
    params: {
        id: 1
    },
    // scope: this, // you can set the scope too
    callback: function(opt,success,respon){
        test2();
    } 
});

Note: This method doesn't work with ExtJS 3.

Solution 3

ExtJS does not provide synchronous request capabilities out of the box. In fact, they strongly discourage using them.

You may find the following discussion helpful.

Share:
44,761
Jemin
Author by

Jemin

I am a software developer working on various technology such as C#, VB .net. ASP .Net, HTML, CSS, AJAX, LINQ ... I am very enthusiastic to learn the new technology. As a developer we face many problems as things dont work as it should be, I hope stackoverflow community helps me and develop my knowledge further.

Updated on June 19, 2020

Comments

  • Jemin
    Jemin about 4 years

    How can I make synchronous ajax requests in EXT JS?

    For example, given this code:

    test1();
    ajaxRequest(); //Ajax Request
    test2();
    

    The test2 function is executed without even finishing the execution of ajaxRequest(), which has an Ext.Ajax.request call .

    How can I make text2() execute only after the ajaxRequest() function has been executed?

    I understand that one way of doing it is to call the test2 function in a callback, but I have some dependencies and a lot of code that has to be executed after the ajax request, in a synchronous manner. Can you please help me with the best solution?

  • Meredith
    Meredith almost 11 years
    This is a 2007 discussion.
  • dansalmo
    dansalmo almost 10 years
    The async attribute is documented for Ext.Ajax.request in the 5.0 release. See docs.sencha.com/extjs/5.0/apidocs/#!/api/Ext.Ajax
  • dansalmo
    dansalmo almost 10 years
    The async attribute is now documented for Ext.Ajax.request in the 5.0 release. See docs.sencha.com/extjs/5.0/apidocs/#!/api/Ext.Ajax
  • Amol Pujari
    Amol Pujari over 7 years
    for 3.4 I dont find async been used