jquery ajax调用success,如何在包装器javascript函数中更改全局变量?

时间:2022-12-08 20:43:51
function ajax_test(str1){ 
  var url = "None" 
  jq.ajax({
    type:'post', 
    cache: false, 
    url: 'http://....' + str1, 
    success: function(data, status, xhr){ 
      url=data; 
    }, 
    error: function (xhr, status, e) {  
    }, 
    async: true, 
    dataType: 'json' 
  }); 
  return url 
} 

How can I set the global variable url to be the returned success ajax data?

如何将全局变量url设置为返回的成功ajax数据?

2 个解决方案

#1


25  

In Javascript, it is impossible for a function to return an asynchronous result. The function will usually return before the AJAX request is even made.

在Javascript中,函数不可能返回异步结果。该函数通常在AJAX请求完成之前返回。

You can always force your request to be syncronous with async: false, but that's usually not a good idea because it will cause the browser to lock up while it waits for the results.

您总是可以强迫您的请求与async同步:false,但这通常不是一个好主意,因为它会导致浏览器在等待结果时锁定。

The standard way to get around this is by using a callback function.

解决这个问题的标准方法是使用回调函数。

function ajax_test(str1, callback){  
   jq.ajax({ 
     //... your options
     success: function(data, status, xhr){  
       callback(data);
     }
   });  
}  

and then you can call it like this:

然后你可以这样称呼它:

ajax_test("str", function(url) {
  //do something with url
});

#2


2  

Here is my example code for retrieving data from php, and then pass the value to a javascript global variable within ajax success function. It works for me!

下面是我从php检索数据的示例代码,然后将值传递给ajax success函数中的javascript全局变量。跟我想的一样!

var retVal = null; 

function ajaxCallBack(retString){
    retVal = retString;
}

function readString(filename){
    $.ajax({  
        type: "POST",  
        url: "readString.php",  
        data: { 'fn': filename },      
        success: function(response){
            ajaxCallBack(response);
        }
    }); 
}

PHP code (readString.php):

PHP代码(readString.php):

<?php

     $fn  = $_POST['fn'];

     $file = fopen("path/".$fn.".record","r");
     $string = fread($file,filesize("path/".$fn.".record"));
     fclose($file); 

     echo $string;  
?>

However, as $.ajax() sent requests asynchronously, which means it may return before the success callback runs, you should not rely on it runs sequentially and return the value by itself. Therefore, here we assign php response value to global value in callback function.

但是,由于$.ajax()异步发送请求,这意味着它可能会在成功回调运行之前返回,所以不应该依赖它按顺序运行并自己返回值。因此,我们在回调函数中将php响应值赋给全局值。

#1


25  

In Javascript, it is impossible for a function to return an asynchronous result. The function will usually return before the AJAX request is even made.

在Javascript中,函数不可能返回异步结果。该函数通常在AJAX请求完成之前返回。

You can always force your request to be syncronous with async: false, but that's usually not a good idea because it will cause the browser to lock up while it waits for the results.

您总是可以强迫您的请求与async同步:false,但这通常不是一个好主意,因为它会导致浏览器在等待结果时锁定。

The standard way to get around this is by using a callback function.

解决这个问题的标准方法是使用回调函数。

function ajax_test(str1, callback){  
   jq.ajax({ 
     //... your options
     success: function(data, status, xhr){  
       callback(data);
     }
   });  
}  

and then you can call it like this:

然后你可以这样称呼它:

ajax_test("str", function(url) {
  //do something with url
});

#2


2  

Here is my example code for retrieving data from php, and then pass the value to a javascript global variable within ajax success function. It works for me!

下面是我从php检索数据的示例代码,然后将值传递给ajax success函数中的javascript全局变量。跟我想的一样!

var retVal = null; 

function ajaxCallBack(retString){
    retVal = retString;
}

function readString(filename){
    $.ajax({  
        type: "POST",  
        url: "readString.php",  
        data: { 'fn': filename },      
        success: function(response){
            ajaxCallBack(response);
        }
    }); 
}

PHP code (readString.php):

PHP代码(readString.php):

<?php

     $fn  = $_POST['fn'];

     $file = fopen("path/".$fn.".record","r");
     $string = fread($file,filesize("path/".$fn.".record"));
     fclose($file); 

     echo $string;  
?>

However, as $.ajax() sent requests asynchronously, which means it may return before the success callback runs, you should not rely on it runs sequentially and return the value by itself. Therefore, here we assign php response value to global value in callback function.

但是,由于$.ajax()异步发送请求,这意味着它可能会在成功回调运行之前返回,所以不应该依赖它按顺序运行并自己返回值。因此,我们在回调函数中将php响应值赋给全局值。