如何访问jquery ajax调用返回的数据? $ .ajax(...)之外; [英] How can i get access to the data returned from a jquery ajax call? outside the $.ajax(...);

查看:57
本文介绍了如何访问jquery ajax调用返回的数据? $ .ajax(...)之外;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do i get access to the data return from jquery ajax call. Outside the $.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;
}


推荐答案

Ajax以异步方式工作所以你的if在ajax调用结束之前,将发现find语句。

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

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

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

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);
      }
   });

这篇关于如何访问jquery ajax调用返回的数据? $ .ajax(...)之外;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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