简体   繁体   English

jQuery 检查 Ajax 请求响应是否等于特定值不执行任何操作

[英]jQuery checking if Ajax request response equals to specific value does nothing

I'm using jQuery and Ajax to send my form data over to a PHP script and that all works well.我正在使用 jQuery 和 Ajax 将表单数据发送到 PHP 脚本,并且一切正常。 But when I'm trying to check the response value of the Ajax request to determine which SweetAlert text I need to show the user things start not functioning.但是当我试图检查 Ajax 请求的响应值以确定我需要向用户显示哪些 SweetAlert 文本时,事情开始无法正常工作。 When checking if the response is equal to "success" a SweetAlert modal should pop up but nothing happens but in the browser console I can clearly see that the only response from the server is "success".当检查响应是否等于“成功”时,应该会弹出一个 SweetAlert 模式,但没有任何反应,但在浏览器控制台中,我可以清楚地看到来自服务器的唯一响应是“成功”。 However if any of the else if statements are true (meaning the server sent that response that it's looking for) then SweetAlert works just fine.但是,如果任何 else if 语句为真(意味着服务器发送了它正在寻找的响应),那么 SweetAlert 就可以正常工作。 I have used this response checking system for months and it has worked perfectly but a day ago it just stopped working and I didn't update or change anything related to SweetAlert and jQuery.我已经使用这个响应检查系统几个月了,它运行良好,但一天前它刚刚停止工作,我没有更新或更改与 SweetAlert 和 jQuery 相关的任何内容。 My code:我的代码:

$("#createIncidentBtn").click(function(e) { //Kui uue intsidendi loomis nuppu vajutatakse, siis
    e.preventDefault();
    $("#createIncidentBtn").val('Please wait...'); //Muudab uuendamis nupu teksti
    message = simplemde.value();
    $.ajax({
        url: '/status/main/php/dashboard-process.php',
        method: 'POST',
        data: $("#new_incident_form").serialize()+'&action=new_incident'+'&message='+simplemde.value(),
        success:function(response) {
            $("#createIncidentBtn").val('Create Incident'); //Muudab intsidendi loomis nupu teksti
            console.log(response); //The response from the server is "success" (without the quotes)
            if(response === 'success') { //This if statement does not work
                Swal.fire({
                    title: 'New Incident successfully created!', //Näitab modali, mis ütleb, et informatsiooni uuendati edukalt
                    icon: 'success',
                    type: 'success'
                });
            } else if(response === 'No title or message') { //This else if statement works
                Swal.fire({
                    title: 'You forgot to enter a title or message!', //Näitab modali, mis ütleb, et informatsiooni uuendati edukalt
                    icon: 'error',
                    type: 'error'
                });
            } else if(response === 'Invalid incident status') { //This else if statement also works
                Swal.fire({
                    title: 'The incident status you have selected is invalid', //Näitab modali, mis ütleb, et informatsiooni uuendati edukalt
                    icon: 'error',
                    type: 'error'
                });
            }
        }
    });
});

What's weird is that when I change the first if statement to this: if(response.includes('success')) { then it works perfectly and a SweetAlert modal shows up.奇怪的是,当我将第一个 if 语句更改为此: if(response.includes('success')) {然后它完美运行并出现了 SweetAlert 模式。 But the thing is that my PHP script is not outputting anything else besides "success" as I can see in the browser console.但问题是我的 PHP 脚本除了“成功”之外没有输出任何其他内容,正如我在浏览器控制台中看到的那样。 I also checked if any of my other pages are having this issue and they are all having the same issue all of a sudden and I have not changed anything on the other pages.我还检查了我的任何其他页面是否有此问题,并且它们都突然遇到相同的问题,并且我没有更改其他页面上的任何内容。 But I have confirmed that SweetAlert works without the if statement.但是我已经确认 SweetAlert 可以在没有 if 语句的情况下工作。

Thanks for any help, Nimetu.感谢您的帮助,尼米图。

This issue has now been resolved.现在这个问题已经解决了。 It was caused by a trailing whitespace.它是由尾随空格引起的。 I did not find out why the script randomly started outputting a random trailing whitespace even without any echo statement.即使没有任何 echo 语句,我也没有弄清楚为什么脚本会随机开始输出随机尾随空格。 It even did this in parts of the code that had not been edited in months.它甚至在几个月未编辑的部分代码中做到了这一点。 But if you are using Visual Studio Code then this shortcut will delete all trailing whitespaces: Ctrl K + Ctrl X .但是,如果您使用的是 Visual Studio Code,则此快捷方式将删除所有尾随空格: Ctrl K + Ctrl X Which resolved my issue.这解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM