如何访问从jquery ajax调用返回的数据? $ .ajax(...)之外;

时间:2022-10-18 07:51:18

How do i get access to the data return from jquery ajax call. Outside the $.ajax( ).

如何从jquery ajax调用访问数据返回。 $ .ajax()之外。

//Reloads the inital page
function jobexist( jobname )
{
   var dataString = 'jobname=' + jobname;
   var found = false;

   $.ajax(
   {
      type: 'POST',
      url: "/genode/jobs/jobexist.m",
      data: dataString,
      dataType: 'json',
      success: function(data)
      {
         alert("Passed");
         if( data.result == 0 )
         {
            found = true;
          }else{  
            found = false; 
          }
      },
      error: function( data )
      {
         alert("Login Failed");
         return -1; //alert(data);
      }
   });

   if( found == true )
   {
       alert("found!")'
       return true;
   }else{
       alert("not found!");
       return false;
    }
}



if( !jobexist(jobname) )
{
    $("#jobname_error").text("This jobname already exist.");
    $("#jobname_error").show();
    return false;
}

2 个解决方案

#1


2  

Ajax works asynchronously so your if found statement will be hit before the ajax call finishes.

Ajax以异步方式工作,因此在ajax调用完成之前将触发if found语句。

What you can do is call a function from inside your ajax success function and pass whatever data you want to it

你可以做的是从你的ajax成功函数中调用一个函数,并传递你想要的任何数据

function found(data){

   if( data.result == 0 )
   {
       alert("found!")
       return true;
   }else{  
       alert("not found!");
       return false;
   }
}

$.ajax(
   {
      type: 'POST',
      url: "/genode/jobs/jobexist.m",
      data: dataString,
      dataType: 'json',
      success: function(data)
      {
         alert("Passed");
         found(data);
      },
      error: function( data )
      {
         alert("Login Failed");
         return -1; //alert(data);
      }
   });

#2


1  

JavaScript is asynchronous, which means you should change your way of thinking a bit, and start mind-twisting journey into the strange world of callbacks and closures. You could simply expose some variable outside of your ajax call scope, and assign data to it, but it's not really what you want to do.

JavaScript是异步的,这意味着你应该改变一下你的思维方式,并开始进入回调和闭包的奇怪世界。您可以简单地在ajax调用范围之外公开一些变量,并为其分配数据,但这并不是您想要做的事情。

What you want to do is:

你想要做的是:

  1. define a callback function:

    定义一个回调函数:

    
    function afterAjax() {
      $("#jobname_error").text("This jobname already exist.");
      $("#jobname_error").show();
      return false;
    };
    

  2. redefine your function responsible for ajax to accept callbackFn as a second parameter:

    重新定义负责ajax的函数以接受callbackFn作为第二个参数:

    
    function jobexist( jobname, callbackFn ) {
    

  3. pass your callback function when executing ajax-responsible function:

    执行ajax-responsible函数时传递回调函数:

    
    jobexist(jobname, afterAjax)
    

  4. execute this callback when the time is right:

    当时间正确时执行此回调:

    
    if( data.result == 0 ) {
       callbackFn();
    }
    

#1


2  

Ajax works asynchronously so your if found statement will be hit before the ajax call finishes.

Ajax以异步方式工作,因此在ajax调用完成之前将触发if found语句。

What you can do is call a function from inside your ajax success function and pass whatever data you want to it

你可以做的是从你的ajax成功函数中调用一个函数,并传递你想要的任何数据

function found(data){

   if( data.result == 0 )
   {
       alert("found!")
       return true;
   }else{  
       alert("not found!");
       return false;
   }
}

$.ajax(
   {
      type: 'POST',
      url: "/genode/jobs/jobexist.m",
      data: dataString,
      dataType: 'json',
      success: function(data)
      {
         alert("Passed");
         found(data);
      },
      error: function( data )
      {
         alert("Login Failed");
         return -1; //alert(data);
      }
   });

#2


1  

JavaScript is asynchronous, which means you should change your way of thinking a bit, and start mind-twisting journey into the strange world of callbacks and closures. You could simply expose some variable outside of your ajax call scope, and assign data to it, but it's not really what you want to do.

JavaScript是异步的,这意味着你应该改变一下你的思维方式,并开始进入回调和闭包的奇怪世界。您可以简单地在ajax调用范围之外公开一些变量,并为其分配数据,但这并不是您想要做的事情。

What you want to do is:

你想要做的是:

  1. define a callback function:

    定义一个回调函数:

    
    function afterAjax() {
      $("#jobname_error").text("This jobname already exist.");
      $("#jobname_error").show();
      return false;
    };
    

  2. redefine your function responsible for ajax to accept callbackFn as a second parameter:

    重新定义负责ajax的函数以接受callbackFn作为第二个参数:

    
    function jobexist( jobname, callbackFn ) {
    

  3. pass your callback function when executing ajax-responsible function:

    执行ajax-responsible函数时传递回调函数:

    
    jobexist(jobname, afterAjax)
    

  4. execute this callback when the time is right:

    当时间正确时执行此回调:

    
    if( data.result == 0 ) {
       callbackFn();
    }