jQuery的Ajax请求不能够将数据恢复到其他功能 [英] Jquery ajax request not able to return data to other function

查看:107
本文介绍了jQuery的Ajax请求不能够将数据恢复到其他功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery一个简单的Ajax请求。下面是我的AJAX功能。

  VAR makeJqueryAjaxRequest =功能(arrParam){
         VAR请求= $阿贾克斯({
             网址:arrParam ['URL'],
             异步:假的,
             类型:arrParam ['类型'],
             数据:arrParam ['数据'],
             数据类型:arrParam ['DATA_TYPE'],
             成功:功能(数据){
                 如果(数据){
                 返回的数据;
                 }
             }
         });
     }
 

这是我的函数调用:

  VAR项目= {
       类型:POST,
       URL:ajaxGetUrl,
       数据:arrParam ['数据'],
       DATA_TYPE:HTML
      };
      VAR味精= makeJqueryAjaxRequest(项目);
 

现在不知道为什么,我的makeJqueryAjaxRequest函数总是返回空值。如果我提醒中取得成功的数据:我得到的数据完美的。但是,当我试图返回它给我的空值

解决方案
  

您无法从异步回调函数的返回值。

由于成功异步回调这就是所谓的jQuery时的ajax事件(成功在这种情况下)火灾。于是久违的东西,从这个功能不会有任何影响,因为他们将返回到jQuery的code。

您可以使用以下

  VAR makeJqueryAjaxRequest =功能(arrParam){
     VAR请求= $阿贾克斯({
        网址:arrParam ['URL'],
        异步:假的,
        类型:arrParam ['类型'],
        数据:arrParam ['数据'],
        数据类型:arrParam ['DATA_TYPE']

     });
   返回请求;
  }
 

然后执行

  makeJqueryAjaxRequest(项目).done(功能(数据){
    如果(数据){
      VAR味精=数据;
      //做任何你喜欢用味精现在
    }
 });
 


替代回调方法:

  VAR makeJqueryAjaxRequest =功能(arrParam,回调){
     VAR请求= $阿贾克斯({
        网址:arrParam ['URL'],
        异步:假的,
        类型:arrParam ['类型'],
        数据:arrParam ['数据'],
        数据类型:arrParam ['DATA_TYPE'],
        成功:功能(数据){
           如果(数据){
              回调(数据);
           }
       }
     });

  }
 

然后使用它像

  makeJqueryAjaxRequest(项目,功能(数据){
    //做任何你想用数据
 });
 

单据上 $。阿贾克斯()

注意

和使用其中任一方法异步:假是没有必要的。您可以删除。由于医生说

  

在jQuery 1.8中,使用异步:假是德precated

I am making a simple ajax request using jquery . Below is my ajax function .

     var makeJqueryAjaxRequest = function(arrParam) {
         var request = $.ajax({
             url : arrParam['url'],
             async: false,
             type: arrParam['type'],
             data: arrParam['data'],
             dataType: arrParam['data_type'],
             success: function(data) {
                 if(data){ 
                 return data;   
                 }
             }
         });
     }

here is my function calls :

      var items = {
       "type" : 'POST',
       "url" : ajaxGetUrl,
       "data" : arrParam['data'],
       "data_type" : 'html'
      };
      var msg = makeJqueryAjaxRequest(items);

Now don't know why my makeJqueryAjaxRequest function always returns the null value. If I alert the data in the success : I'm getting the data perfect . But when I try to return it gives me the null value

解决方案

You can't return value from an Asynchronous callback function.

Because success is a async callback which is called by jQuery when a ajax Event(success in this case) fires. So returning something from this functions will not have any effect as they will be returned to jQuery code.

You can use the following

var makeJqueryAjaxRequest = function(arrParam) {
     var request = $.ajax({
        url : arrParam['url'],
        async: false,
        type: arrParam['type'],
        data: arrParam['data'],
        dataType: arrParam['data_type']

     });
   return request;
  }

Then do

 makeJqueryAjaxRequest(items).done(function(data){
    if(data){
      var msg  = data;  
      // do whatever you like with msg now
    }
 });


Alternative Callback Approach:

var makeJqueryAjaxRequest = function(arrParam,callback) {
     var request = $.ajax({
        url : arrParam['url'],
        async: false,
        type: arrParam['type'],
        data: arrParam['data'],
        dataType: arrParam['data_type'],
        success: function(data) {
           if(data){ 
              callback(data); 
           }
       }
     });

  }

Then use it like

 makeJqueryAjaxRequest(items,function(data){
    // do whatever you like with data
 });

Doc on $.ajax()

Note

And with either of these approach async: false is not necessary. You can remove that. As the doc says

As of jQuery 1.8, the use of async: false is deprecated

这篇关于jQuery的Ajax请求不能够将数据恢复到其他功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆