async:falseに設定すると、関数は正しい値を返しています。しかし、それはブラウザをブロックします。コールバックを追加しようとしましたが、正しい値が返されませんでした。 CALLBACK WITHasyncでJquery ajaxリクエスト:trueが値を返しません
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;
}
そして、次のように私は関数を呼び出す:
arr_one=find_route(arr,find_route2)
しかしarr_oneはundefinedを返します。
EDIT:あなたのコールバックに二つの引数を渡して、一見真
はあなたがいずれかを参照していJavaScriptのエラー? – jrummell
"同期Ajax要求"はコード実行の速度を低下させますか? –
jensでは、メインの実行スレッドがロックされ、jsはajaxリクエストが戻ってくるのを待っている間は何もできません。 –