Javascript EXT Js 同步ajax请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6233071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:52:27  来源:igfitidea点击:

EXT Js Synchronous ajax request

javascriptajaxfunctionextjs

提问by Jemsworld

How can I make synchronous ajax requests in EXT JS?

如何在 EXT JS 中进行同步 ajax 请求?

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.requestcall .

test2 函数甚至没有完成 的执行就被执行了ajaxRequest(),它有一个Ext.Ajax.request调用。

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

如何text2()仅在ajaxRequest()函数执行后才执行?

I understand that one way of doing it is to call the test2function 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?

我知道这样做的一种方法是test2在回调中调用该函数,但是我有一些依赖项和许多必须在 ajax 请求之后以同步方式执行的代码。你能帮我找到最好的解决方案吗?

回答by Redder

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 asyncattribute, on the request()method, so, it's actually possible to make a synchronous request, like this:

我需要类似的东西和看的源代码后Ext.Ajax.request()Ext.data.Connection,我发现他们检查一个async属性上request()的方法,所以,它实际上是可以进行同步请求,就像这样:

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

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

似乎这个async属性没有记录,所以......当然......请注意它可能会在未来的版本中发生变化。

回答by Egy Mohammad Erdin

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

Extjs.Ajax每个 AJAX 调用中都会产生三种类型的回调函数之一:

  • successthat runs if the AJAX call is successfully created and gets a response
  • failurethat runs if the AJAX call fails to get a response
  • callbackthat runs AFTER the AJAX call gets a response
  • success如果成功创建 AJAX 调用并获得响应,则运行
  • failure如果 AJAX 调用无法获得响应,则运行
  • callback在 AJAX 调用得到响应后运行

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

如果要执行test2();您的Ajax请求完成后,
然后把你test2的函数内部successfailurecallback功能; 选择哪一个,由你决定……

More info on the successor failurefunction can be found here.

可以在此处找到有关successorfailure函数的更多信息。

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.

注意:此方法不适用于 ExtJS 3。

回答by Li0liQ

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

ExtJS 不提供开箱即用的同步请求功能。事实上,他们强烈反对使用它们。

You may find the followingdiscussion helpful.

您可能会发现以下讨论很有帮助。