【问题标题】:Return true in jquery ajax在 jquery ajax 中返回 true
【发布时间】:2014-06-21 21:43:52
【问题描述】:

我在 jquery 中使用 ajax 来检查数据库中是否存在该值。我们可以为此使用普通的选择查询(php),但如果值存在,我必须弹出警报。这就是为什么我决定这样。我几乎完成了,但问题是由于在 jquery 中返回 false,即使数据库中不存在名称,它也不允许下一行。我怎样才能在那个特定的地方返回 true?

$.ajax({
    type:"post",
    url:"actionn.php",
    data:"name="+name,
    success:function(data){
        alert(data);        
        //if data does not exist means it should get out of ajax function            
    }
});
return false; //if data exists

if(name=="") //data does not exist means it should come here
{
    alert("Please enter your Name");
    return false;
}

【问题讨论】:

  • 也许它被复制和粘贴错了,但在我看来,您在提交 ajax 调用后return false。您可能希望将代码移动到 success 回调中...?
  • 我认为您希望 return true 在您的成功回电中 - 可能在警报之前。擦除return false
  • @t.niese 是对的 - 您想要的答案在他/她提供的链接中 - 在答案中的“解决方案”之后向下查看页面。
  • 我想返回 true.. 如果 data="Name 已经存在" 意味着返回 false。但是如果 data="" 意味着它必须继续下一行。

标签: jquery ajax


【解决方案1】:

正如 t.niese 指出的那样,success 内部的return true(或错误)并没有按照您的预期进行。 (另请参阅您的问题下的第一条评论)。

如果用户(不)存在于success 中,可能最直接的实现目标的方法是使用您拥有的任何代码。

所以基本上移动逻辑/代码当用户在success 回调中存在/不存在时要做什么。
要获得更多见解,您真的应该阅读 cmets 中提供的一些链接

编辑:
我认为您错过了理解 异步 请求的概念 - 考虑到代码中的 cmets

$.ajax({
    type:"post",
    url:"actionn.php",
    data:"name="+name,
    success:function(data){
        alert(data);        
        //if data does not exist means it should get out of ajax function
        //ajax function has already "exited" here is the success callback
        //this code is executed when the request to action.php was succesfull
        //as in response code is 200/OK

    }
});
//return false; //if data exists
//nope, you don't know if data exists this code will be
//executed _while_ (!) a request to action.php is made!
//there is no guarantee when this request returns and you can't write code
//here which relies on the response

//if(name=="") //data does not exist means it should come here
//{
//    alert("Please enter your Name");
//    return false;
//}

你真正想写的是这样的:

$.ajax({
    type:"post",
    url:"actionn.php",
    data:"name="+name,
    success:function(data){
        if(name==""){
            alert("Please enter your Name");
        }else{
            alert("Your name is: "+data);
        }
    }
});

又脆又短。所有取决于服务器响应的逻辑都在内部 successcallack 处理。没有代码after $.ajax 以任何方式依赖于data

请不要在$.ajax.success 中使用returning - 这不是你认为的那样

对于更多问题,我想(再次)邀请您阅读有关 Ajax 调用的内容:https://stackoverflow.com/a/14220323/1063730 我认为关于重组代码的段落对您来说非常有趣。

【讨论】:

  • ,它有效。但是我如何放置 if(data == ""){ return false; }else{ 返回真;它不起作用? }
  • 当您尝试在success 回调中使用return 时,我认为您对概念有误解。我编辑了我的答案,希望它会更容易看到。
【解决方案2】:

放这个

返回假; //如果数据存在

在此

success:function(data){
   alert(data);        
   //if data does not exist means it should get out of ajax function     
   return false; //if data exists - //LIKE THIS
}

【讨论】:

  • 我想他想要return true
  • 您不能以这种方式从success 回调中返回某些内容。所以return false; 没有效果。
【解决方案3】:

您可以在成功函数中使用if 条件,例如:

success:function(data) { 
    if(data) { 
         alert(data); 
    }  
    else { 
         alert("Please enter your Name"); 
         return; 
     }     
 }

查看$.ajax的文档

【讨论】:

  • 小心:当 HTTP 通信失败、超时、404、500 等时调用 error...根据您的 API,您可能会得到一个很好的响应 200/OK,但您需要检查一个空字符串在success 中(就像你建议的那样)~ 只是不完全一样。 :)
  • 是的,你是对的。我正在修改我的答案。谢谢。
  • 您不能以这种方式从success 回调中返回某些内容。所以return false; 对你的代码没有影响。
  • 建议error 并不是完全错误的。如果服务器正在使用更多 HTTP 的可能性,你可以例如接收401/Unauthorised403/Forbidden 作为响应。减少 success 中的 if-else 需求
  • @Ansuraj Khadanga,ajax 已经打电话了。即 data="name 已存在" 它处于 if 条件。在其他情况下,它不应弹出任何窗口并继续下一行(返回)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-11
  • 1970-01-01
  • 1970-01-01
  • 2011-08-28
  • 2017-02-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多