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
我在我的AJAX中有一个成功函数,它从python脚本返回响应文本,该脚本可以是“SUCCESS”或“EMPTY”。现在我想在成功函数中放置一个if循环,但是if循环不起作用。我从我的python脚本中获取正确的数据,因为我的警报语句工作正常并打印“SUCCESS”。但它没有进入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:
我已经尝试了很多东西,但是控件没有进入if循环,有人可以告诉我我做错了什么:
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;
}
3 个解决方案
#1
9
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
:
或者使用jQuery的$ .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
}
#2
0
check if you don't have any additional spaces before/after "SUCCESS"
检查“SUCCESS”之前/之后是否没有任何额外空格
And also try to change
并尝试改变
if(data === "SUCCESS"){
to
至
if(data == "SUCCESS"){
#3
-1
submitHandler: function (form) {
$.ajax({
type: 'post',
url: '/cgi-bin/getdataworld.py',
data: $(form).serialize(),
success: function(data1) {
//document.write(result);
console.log("result is "+data1);
alert(data1);
if(data1 == "SUCCESS"){
window.location = 'index.html';
}
else{
alert("NO DATA PRESENT");
}
},
error: function (responseData) {
console.log('Ajax rariequest not recieved!');
}
});
return false;
}
try this here two things i noticed one is === in if statement and other you are using data as variable some time its conflicts in some browser
在这里尝试这两件事我注意到一个是===在if语句和其他你使用数据作为变量一段时间它在某些浏览器中的冲突
#1
9
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
:
或者使用jQuery的$ .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
}
#2
0
check if you don't have any additional spaces before/after "SUCCESS"
检查“SUCCESS”之前/之后是否没有任何额外空格
And also try to change
并尝试改变
if(data === "SUCCESS"){
to
至
if(data == "SUCCESS"){
#3
-1
submitHandler: function (form) {
$.ajax({
type: 'post',
url: '/cgi-bin/getdataworld.py',
data: $(form).serialize(),
success: function(data1) {
//document.write(result);
console.log("result is "+data1);
alert(data1);
if(data1 == "SUCCESS"){
window.location = 'index.html';
}
else{
alert("NO DATA PRESENT");
}
},
error: function (responseData) {
console.log('Ajax rariequest not recieved!');
}
});
return false;
}
try this here two things i noticed one is === in if statement and other you are using data as variable some time its conflicts in some browser
在这里尝试这两件事我注意到一个是===在if语句和其他你使用数据作为变量一段时间它在某些浏览器中的冲突