[英]if else jQuery not working with ajax response
我有以下代码:
$.ajax({
url: 'upload.php', //Server script to process data
type: 'POST',
xhr: function() { // Custom XMLHttpRequest
var myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){ // Check if upload property exists
myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // For handling the progress of the upload
}
return myXhr;
},
//Ajax events
beforeSend: function(e) { $('#progress').css('display', 'block'); },
success: function(e) {
if (e != "bad" || e != "No") {
alert('File Upload Success!');
$('#imgsrc').attr("src", e);
$('#img').val(e);
} else {
alert('File Failed, Upload File of Size < 1MB');
}
} ,
error: function(e) { alert('error' + e.message); } ,
// Form data
data: formData,
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false
});
而不是去其他地方,因为回应bad
& No
。 它显示File Upload Success!
并在其他故障时将图像src
添加为bad
和No
如果不是,我在做错什么吗?
从upload.php的回应是: - FileName
, bad
,和No
。
谢谢
您的||
应该是&&
。
您是在说:如果响应不是这个或不是–按照定义,这总是正确的。
您需要使用&&
,因为您正在使用或( ||
)
如果e
为Bad
,则它not No
...。如果e
为No
,则它not Bad
...所以No
或Bad
总是会过滤掉。
if (e != "bad" || e != "No") {
应该...
if (e != "bad" && e != "No") {
e是No或Bad 。 您的if语句始终为true,因为当e为No时,它显然不是Bad ,您可以使用or(||)运算符。 因此,您说的是:“如果e不为No或Bad,则将其设为true。
采用:
if (e != "bad" && e != "No") {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.