与异步的jQuery Ajax请求:真的没有返回值 [英] Jquery ajax request with async:true does not return value

查看:196
本文介绍了与异步的jQuery Ajax请求:真的没有返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用异步:设置为false,该函数返回正确的值。然而,它的块的浏览器。我试图添加回调,但它没有返回正确的值!

With async:set to false, the function is returning the correct value. However it blocks the browser. I've tried to add callbacks but it not returning the correct value!

function find_route(array_bustops){
        var myresults;
            $.ajax({

            type: 'POST',
                async: true,  //with async:false it works
            url: 'find_routenum.php',

            data:{

                array_bustops:JSON.stringify(array_bustops)

            },
            dataType:'json', //html,xml

            success: function(my_results){

                myresults=my_results;


            },

            error:function(x,e){
                if(x.status==0){
                alert('You are offline!!\n Please Check Your Network.');
                }else if(x.status==404){
                alert('Requested URL not found.');

            }


        });

        return myresults;
    }

使用回拨:

function find_route(array_bustops,callback){
        var myresults;
            $.ajax({

            type: 'POST',
                async: true,  //with async:false it works
            url: 'find_routenum.php',

            data:{

                array_bustops:JSON.stringify(array_bustops)

            },
            dataType:'json', //html,xml

            success: function(my_results){

                callback(array_bustops,my_results)


            },

            error:function(x,e){
                if(x.status==0){
                alert('You are offline!!\n Please Check Your Network.');
                }else if(x.status==404){
                alert('Requested URL not found.');

            }


        });

        return myresults;
    }
    function find_route2(myresults){
           return myresults;
    }

然后我调用该函数如下:

And then i call the function as follows:

arr_one=find_route(arr,find_route2)

但arr_one返回undefined。

but arr_one returns undefined.

编辑:仍无法与异步工作:设置为true

it still is not working with async:set to true

推荐答案

arr_one = find_route(ARR,find_route2)

arr_one=find_route(arr,find_route2)

这什么都没有做与异步但与code的架构。

this got nothing to do with async but with the architecture of your code.

您在find_route范围申报myresults,当你调用find_route函数返回因为你只delcare它谁是未定义myresults的价值。

you declare myresults in the scope of find_route, when you call find_route the function return the value of myresults who is undefined because you delcare it only.

试图声明VAR myresults ='someValue中';在你的函数

Try to declare var myresults='somevalue'; in your function

现在你的函数返回someValue中

Now your function return 'somevalue'

这发生,因为调用AJAX方法将不会停止执行的功能和成功的方法将被执行的方式find_route被称为并返回只是把的console.log在那里你会看到,他们最终都会在登录后成功之称。

This happen because the call to ajax method will not stop the function execution and the success method will be executed way after find_route is called and returned just put a console.log in there you'll see that it will get log eventually when the success is called.

异步假时,设置它的工作,因为异步错误的,因为你会发现从返回停止功能执行之前Ajax调用回来

It work when async false is set because async false as you discover stop the function execution from returning before the ajax call is back

编辑:可能的解决方案。

possible solution

function DataListener(val){
    this.val = val;
    this.remoteFetch();
}

DataListener.prototype.set = function(val){
    this.val = val;
    this.notifyListener(val)
}
DataListener.prototype.get = function(val){
    return this.val;
}

DataListener.prototype.remoteFetch = function(){
    $.ajax({
        success:$.proxy(this, 'set')
    })
}

var arr_one = new DataListener('somevalue');
alert(arr_one.get());// alert somevalue

function doSomething(arr_one_value){
    alert(arr_one_value); // the new value set from the ajax call   
}

arr_one.addListener(doSomething);

这篇关于与异步的jQuery Ajax请求:真的没有返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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