jQuery的:如果语句不工作里面的ajax成功函数 [英] jquery: If statement not working inside ajax success function

查看:96
本文介绍了jQuery的:如果语句不工作里面的ajax成功函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个成功的功能在我的AJAX返回从一个python脚本它可以是成功或空的响应文本。 现在我想将一个如果环成功函数内部,但如果环不工作。我从我的python脚本得到正确的数据,因为我的提醒语句是工作的罚款,并打印成功。但它不进入ifloop

我已经尝试了一堆东西,但控制不进入,如果环,任何人都可以告诉我什么,我做错了:

  submitHandler:功能(形式){

                $阿贾克斯({
                    类型:'后',
                    网址:/cgi-bin/getdataworld.py,
                    数据:$(形式).serialize()

                    成功:功能(数据){
                            //document.write(result);
                            的console.log(结果为+数据);
                            警报(数据);

                            如果(数据===SUCCESS){
                            了window.location ='的index.html';
                               }
                           其他{
                                 警报(NO DATA preSENT);
                               }


                    },

                    错误:函数(responseData){
                的console.log('Ajax请求没有收到!);
            }

                });

                返回false;
            }
 

解决方案

这意味着,你回答什么是不是SUCCESS。它可能有前或换行或其他的空白后,可能不止一个。

也许

 如果(/^\s*SUCCESS\s*$/.test(data)){
    // 成功
}
 

或者使用jQuery的 $修剪

 如果($ .trim(数据)===SUCCESS){
    // 成功
}
 

另外要确保你不被成功成功,答复为字符串比较是区分大小写的。

如果你不知道:

 如果(/^\s*SUCCESS\s*$/i.test(data)){
    // 成功
}
 

 如果($ .trim(数据).toUpperCase()===SUCCESS){
    // 成功
}
 

I have a success function in my AJAX which returns the response text from a python script which can be either "SUCCESS" or "EMPTY". Now I want to place an if-loop inside the success function, but the if-loop is not working. I am getting the correct data from my python script because my alert statement is working fine and printing "SUCCESS". But it does not enter the ifloop

I have tried a bunch of things but the control is not entering the if-loop, could anyone please tell me what I am doing wrong:

submitHandler: function (form) {

                $.ajax({
                    type: 'post',
                    url: '/cgi-bin/getdataworld.py',
                    data: $(form).serialize(),

                    success: function(data) {
                            //document.write(result);
                            console.log("result is "+data);
                            alert(data);

                            if(data === "SUCCESS"){
                            window.location = 'index.html';
                               }
                           else{
                                 alert("NO DATA PRESENT");
                               }


                    },

                    error: function (responseData) {
                console.log('Ajax request not recieved!');
            }

                });

                return false;
            }

解决方案

This means that what you're responding with isn't "SUCCESS". It probably has a line break or other whitespace before or after it, maybe more than one.

Perhaps:

if (/^\s*SUCCESS\s*$/.test(data)) {
    // Success
}

Or use jQuery's $.trim:

if ($.trim(data) === "SUCCESS") {
    // Success
}

Also be sure that you're not replying with "Success" or "success", as string comparisons are case-sensitive.

If you're not sure:

if (/^\s*SUCCESS\s*$/i.test(data)) {
    // Success
}

or

if ($.trim(data).toUpperCase() === "SUCCESS") {
    // Success
}

这篇关于jQuery的:如果语句不工作里面的ajax成功函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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