Javascript Ajax 成功 if 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12608089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 10:49:00  来源:igfitidea点击:

Ajax success if statement

javascriptjqueryajax

提问by Joe

When the user clicks on a remove button, the code sends a request to the server to remove that user from the DB first and then from the table list on the screen.

当用户单击删除按钮时,代码会向服务器发送请求,先从数据库中删除该用户,然后再从屏幕上的表列表中删除该用户。

My problem is that a if else statment withint the ajax success does return what I want.

我的问题是 ajax 成功中的 if else 语句确实返回了我想要的。

I can't add this code to fiddlers because it's linking to a database which is not accessible online.

我无法将此代码添加到提琴手,因为它链接到无法在线访问的数据库。

$(nTd).children('a').click(function(e) {
    e.preventDefault();
    var that = $(this);
    if(that.hasClass('icon-i-remove')){
       $.ajax({
          url: $(this).attr('href'),
          type: $(this).attr('method'),
          dataType: 'json',
          success: function (data) {
            //PROBLEM HERE **********
            if(data.success == 'true' ){
               alert('true');
            } else {
               alert('false')
            };
       ;}
   });

So, 2 major problems. The if statement doesn't run correctly. even if it return true, the server jumps to the else without caring about it.

所以,2个主要问题。if 语句运行不正确。即使它返回true,服务器也会不关心它而跳转到else。

The second issue is this. When the user clicks on the remove button, the code should first check if the user has been deleted on the DB. If I have the ok from the DB, then I can remove the row from the table list without refreshing the page, otherwise I should display an error message (false return)

第二个问题是这样的。当用户点击删除按钮时,代码应该首先检查用户是否已在数据库上被删除。如果我从数据库中获得了 ok,那么我可以在不刷新页面的情况下从表列表中删除该行,否则我应该显示一条错误消息(假返回)

In my case, when I click remove, the code send a request to the server and doesn't care what the result is.

在我的例子中,当我点击删除时,代码会向服务器发送一个请求并且不关心结果是什么。

I need the code to wait before executing the client side remove. I need to know if that user has been removed from the db or not. I find it difficult to manage the if statement do. It seems it's not looking at the result back from the backend.

我需要代码在执行客户端删除之前等待。我需要知道该用户是否已从数据库中删除。我发现很难管理 if 语句 do。似乎它没有从后端查看结果。

True or false it skips through either way. What's going wrong?

无论是真还是假,它都会跳过任何一种方式。怎么了?

回答by Barry Chapman

Is it actually returning 'true'? Or is it returning true? Two different things

它真的回来了'true'吗?还是它回来了true?两种不同的东西

UPDATE

更新

$(nTd).children('a').click(function(e) {
    e.preventDefault();
    var that = $(this);
    if(that.hasClass('icon-i-remove')){
       $.ajax({
          url: $(this).attr('href'),
          type: $(this).attr('method'),
          dataType: 'json',
          success: function (data) {
            //PROBLEM HERE **********
            if(data.success == true ){   <-- Change to true (remove quotes)
               alert('true');
            } else {
               alert('false')
            };
       ;}
   });

回答by ilan berci

If your ajax success callback is executing.. it means the server side operation was completed SUCCESFULLY.

如果您的 ajax 成功回调正在执行..这意味着服务器端操作已成功完成。

If not, then the server should not send back a 200 that will get interpreted as a success

如果不是,那么服务器不应该发回一个 200 将被解释为成功