jQuery将ajax结果返回到外部变量

时间:2022-04-28 15:42:14

I have some problem using ajax.

使用ajax有一些问题。

How can I assign all result from ajax into outside variable ?

如何将所有的结果由ajax分配到外部变量?

I google it up and found this code..

我找到了这个密码。

var return_first = (function () {
    var tmp = null;
    $.ajax({
        'async': false,
        'type': "POST",
        'global': false,
        'dataType': 'html',
        'url': "ajax.php?first",
        'data': { 'request': "", 'target': arrange_url, 'method': method_target },
        'success': function (data) {
            tmp = data;
        }
    });
    return tmp;
});

but not work for me..

但不是为我工作。

Can anybody tell what is wrong about that code ?

有人知道这段代码有什么问题吗?

3 个解决方案

#1


32  

You are missing a comma after

你后面少了一个逗号

'data': { 'request': "", 'target': 'arrange_url', 'method': 'method_target' }

Also, if you want return_first to hold the result of your anonymous function, you need to make a function call:

此外,如果您希望return_first保存匿名函数的结果,则需要进行函数调用:

var return_first = function () {
    var tmp = null;
    $.ajax({
        'async': false,
        'type': "POST",
        'global': false,
        'dataType': 'html',
        'url': "ajax.php?first",
        'data': { 'request': "", 'target': 'arrange_url', 'method': 'method_target' },
        'success': function (data) {
            tmp = data;
        }
    });
    return tmp;
}();

Note () at the end.

注意()在最后。

#2


20  

This is all you need to do:

这就是你需要做的:

var myVariable;

$.ajax({
    'async': false,
    'type': "POST",
    'global': false,
    'dataType': 'html',
    'url': "ajax.php?first",
    'data': { 'request': "", 'target': 'arrange_url', 'method': 'method_target' },
    'success': function (data) {
        myVariable = data;
    }
});

NOTE: Use of "async" has been depreciated. See https://xhr.spec.whatwg.org/.

注意:“异步”的使用已经贬值。见https://xhr.spec.whatwg.org/。

#3


14  

Using 'async': false to prevent asynchronous code is a bad practice,

使用“async”:false来防止异步代码是一个糟糕的实践,

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. https://xhr.spec.whatwg.org/

不赞成主线程上的同步XMLHttpRequest,因为它会对最终用户的体验产生不利影响。https://xhr.spec.whatwg.org/

On the surface setting async to false fixes a lot of issues because, as the other answers show, you get your data into a variable. However, while waiting for the post data to return (which in some cases could take a few seconds because of database calls, slow connections, etc.) the rest of your Javascript functionality (like triggered events, Javascript handled buttons, JQuery transitions (like accordion, or autocomplete (JQuery UI)) will not be able to occur while the response is pending (which is really bad if the response never comes back as your site is now essentially frozen).

在表面上,将async设置为false会修复很多问题,因为,正如其他答案所示,您将数据放入一个变量中。然而,在等待post数据返回(在某些情况下可能需要几秒钟,因为数据库调用、缓慢的连接,等等)的Javascript功能(如触发事件,Javascript处理按钮,JQuery转换(像手风琴一样,或自动完成(JQuery UI))将不能发生反应时等待(这是非常糟糕的,如果反应,永不再来你的网站现在基本上冻结)。

Try this instead,

试试这个相反,

var return_first;
function callback(response) {
  return_first = response;
  //use return_first variable here
}

$.ajax({
  'type': "POST",
  'global': false,
  'dataType': 'html',
  'url': "ajax.php?first",
  'data': { 'request': "", 'target': arrange_url, 'method': method_target },
  'success': function(data){
       callback(data);
  },
});

#1


32  

You are missing a comma after

你后面少了一个逗号

'data': { 'request': "", 'target': 'arrange_url', 'method': 'method_target' }

Also, if you want return_first to hold the result of your anonymous function, you need to make a function call:

此外,如果您希望return_first保存匿名函数的结果,则需要进行函数调用:

var return_first = function () {
    var tmp = null;
    $.ajax({
        'async': false,
        'type': "POST",
        'global': false,
        'dataType': 'html',
        'url': "ajax.php?first",
        'data': { 'request': "", 'target': 'arrange_url', 'method': 'method_target' },
        'success': function (data) {
            tmp = data;
        }
    });
    return tmp;
}();

Note () at the end.

注意()在最后。

#2


20  

This is all you need to do:

这就是你需要做的:

var myVariable;

$.ajax({
    'async': false,
    'type': "POST",
    'global': false,
    'dataType': 'html',
    'url': "ajax.php?first",
    'data': { 'request': "", 'target': 'arrange_url', 'method': 'method_target' },
    'success': function (data) {
        myVariable = data;
    }
});

NOTE: Use of "async" has been depreciated. See https://xhr.spec.whatwg.org/.

注意:“异步”的使用已经贬值。见https://xhr.spec.whatwg.org/。

#3


14  

Using 'async': false to prevent asynchronous code is a bad practice,

使用“async”:false来防止异步代码是一个糟糕的实践,

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. https://xhr.spec.whatwg.org/

不赞成主线程上的同步XMLHttpRequest,因为它会对最终用户的体验产生不利影响。https://xhr.spec.whatwg.org/

On the surface setting async to false fixes a lot of issues because, as the other answers show, you get your data into a variable. However, while waiting for the post data to return (which in some cases could take a few seconds because of database calls, slow connections, etc.) the rest of your Javascript functionality (like triggered events, Javascript handled buttons, JQuery transitions (like accordion, or autocomplete (JQuery UI)) will not be able to occur while the response is pending (which is really bad if the response never comes back as your site is now essentially frozen).

在表面上,将async设置为false会修复很多问题,因为,正如其他答案所示,您将数据放入一个变量中。然而,在等待post数据返回(在某些情况下可能需要几秒钟,因为数据库调用、缓慢的连接,等等)的Javascript功能(如触发事件,Javascript处理按钮,JQuery转换(像手风琴一样,或自动完成(JQuery UI))将不能发生反应时等待(这是非常糟糕的,如果反应,永不再来你的网站现在基本上冻结)。

Try this instead,

试试这个相反,

var return_first;
function callback(response) {
  return_first = response;
  //use return_first variable here
}

$.ajax({
  'type': "POST",
  'global': false,
  'dataType': 'html',
  'url': "ajax.php?first",
  'data': { 'request': "", 'target': arrange_url, 'method': method_target },
  'success': function(data){
       callback(data);
  },
});