我有一个成功的功能在我的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
}