使用ajax/jquery调用成功函数之外的变量。

时间:2022-03-31 20:21:01

I have the following code

我有以下代码

var test;
     $.ajax({
        type: "GET",
        url: "../views/person/controller.php?actor=person&action=checkAge",
        data: "age=" + value,
        success: function(msg){
            console.log(msg);
            test = msg; 
        },
    });
    Validate.fail(test);

Now the test var should give true of false like the console does say. But test var gives me undefined why?

现在测试var应该像控制台所说的那样,给出true of false。但是测试var给了我未定义的原因?

5 个解决方案

#1


19  

Probably because Validate.fail(test) occurs immediately after the asynchronous call. Remember it is ASYNCHRONOUS, meaning it executes parallel to javascript running on your page.

可能是因为Validate.fail(测试)会在异步调用之后立即发生。请记住它是异步的,这意味着它与在页面上运行的javascript并行执行。

#2


21  

 var test;  <-- (1) This code runs first  
 $.ajax({   <-- (2) Then this runs  
    type: "GET",
    url: "../views/person/controller.php?actor=person&action=checkAge",
    data: "age=" + value,
    success: function(msg){
        console.log(msg); <-- (4) Finally this is run. IF your request is a success 
        test = msg; 
    },
 });
 Validate.fail(test); <-- (3) This runs third  

Look at the order in which the code runs. Your variable is simply not available at that point because it's running when the code is triggered via the callback

看看代码运行的顺序。此时变量不可用,因为当代码通过回调被触发时它正在运行

#3


3  

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var xm;

  $("#txt").ajaxComplete(function(){
    $('#txt').html(xm);
  });

  $("button").click(function(){

    $.ajax({
      url: 'DBresult_load.php',
      dataType: 'html',
      data: { },                 //you can pass values here
      success: function(result) {xm =result;}
    });
  });


});
</script>
</head>
<body>

<div id="txt"><h2>Let AJAX change this text</h2></div>
<button>Change Content</button>
</body>
</html>

Here is the solution for passing values to variable from Ajax request. Hope this helps.

下面是将值从Ajax请求传递给变量的解决方案。希望这个有帮助。

#4


1  

enter code here var test;
 $.ajax({
    type: "GET",
    async: false,
    url: "../views/person/controller.php?actor=person&action=checkAge",
    data: "age=" + value,
    success: function(msg){
        console.log(msg);
        test = msg; 
    },
});
Validate.fail(test);

//Make your ajax function synchronous, set the json parameter "async: false", so javascript has to wait until test is assigned a value.

//使您的ajax函数保持同步,设置json参数“async: false”,因此javascript必须等到测试被赋值。

#5


-1  

What happens when you remove "var" before the term "test" when you declare it?

在声明“test”之前删除“var”会发生什么?

I'm not sure how the call back function is treated with jQuery , as it is wrapped within a few other extended methods.. But the reason i say leave var out in the declaration of the test variable is that var assigns test to be relative to the scope. If your callback is being treated in a certain way, you may lose the scope where test is defined. You may want to drop the var assignment and leave it as a global variable. Perhaps this will make it visible?

我不确定如何使用jQuery处理回调函数,因为它包含在其他一些扩展方法中。但是我之所以在测试变量的声明中省略var,是因为var将test赋值为相对于作用域的值。如果回调以某种方式处理,您可能会失去定义测试的范围。您可能希望删除var赋值并将其保留为全局变量。也许这会让它变得可见?

EDIT:: Didn't realize you were referencing the term within a function call after the async request -- i would suggest including the last statement within your callback.

编辑:::没有意识到您在异步请求后的函数调用中引用了术语——我建议在回调中包含最后一条语句。

:)

:)

#1


19  

Probably because Validate.fail(test) occurs immediately after the asynchronous call. Remember it is ASYNCHRONOUS, meaning it executes parallel to javascript running on your page.

可能是因为Validate.fail(测试)会在异步调用之后立即发生。请记住它是异步的,这意味着它与在页面上运行的javascript并行执行。

#2


21  

 var test;  <-- (1) This code runs first  
 $.ajax({   <-- (2) Then this runs  
    type: "GET",
    url: "../views/person/controller.php?actor=person&action=checkAge",
    data: "age=" + value,
    success: function(msg){
        console.log(msg); <-- (4) Finally this is run. IF your request is a success 
        test = msg; 
    },
 });
 Validate.fail(test); <-- (3) This runs third  

Look at the order in which the code runs. Your variable is simply not available at that point because it's running when the code is triggered via the callback

看看代码运行的顺序。此时变量不可用,因为当代码通过回调被触发时它正在运行

#3


3  

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var xm;

  $("#txt").ajaxComplete(function(){
    $('#txt').html(xm);
  });

  $("button").click(function(){

    $.ajax({
      url: 'DBresult_load.php',
      dataType: 'html',
      data: { },                 //you can pass values here
      success: function(result) {xm =result;}
    });
  });


});
</script>
</head>
<body>

<div id="txt"><h2>Let AJAX change this text</h2></div>
<button>Change Content</button>
</body>
</html>

Here is the solution for passing values to variable from Ajax request. Hope this helps.

下面是将值从Ajax请求传递给变量的解决方案。希望这个有帮助。

#4


1  

enter code here var test;
 $.ajax({
    type: "GET",
    async: false,
    url: "../views/person/controller.php?actor=person&action=checkAge",
    data: "age=" + value,
    success: function(msg){
        console.log(msg);
        test = msg; 
    },
});
Validate.fail(test);

//Make your ajax function synchronous, set the json parameter "async: false", so javascript has to wait until test is assigned a value.

//使您的ajax函数保持同步,设置json参数“async: false”,因此javascript必须等到测试被赋值。

#5


-1  

What happens when you remove "var" before the term "test" when you declare it?

在声明“test”之前删除“var”会发生什么?

I'm not sure how the call back function is treated with jQuery , as it is wrapped within a few other extended methods.. But the reason i say leave var out in the declaration of the test variable is that var assigns test to be relative to the scope. If your callback is being treated in a certain way, you may lose the scope where test is defined. You may want to drop the var assignment and leave it as a global variable. Perhaps this will make it visible?

我不确定如何使用jQuery处理回调函数,因为它包含在其他一些扩展方法中。但是我之所以在测试变量的声明中省略var,是因为var将test赋值为相对于作用域的值。如果回调以某种方式处理,您可能会失去定义测试的范围。您可能希望删除var赋值并将其保留为全局变量。也许这会让它变得可见?

EDIT:: Didn't realize you were referencing the term within a function call after the async request -- i would suggest including the last statement within your callback.

编辑:::没有意识到您在异步请求后的函数调用中引用了术语——我建议在回调中包含最后一条语句。

:)

:)