John Cargo
John Cargo

Reputation: 2121

if else jQuery not working with ajax response

I have below code :

$.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
});

instead of going to else, on response bad & No. It displays File Upload Success! and add image src as bad and No, on different failure.

Am i doing something wrong with If else :-

Response from upload.php are :- FileName, bad , & No.

Thanks

Upvotes: 1

Views: 546

Answers (3)

Krasimir
Krasimir

Reputation: 13529

e is either No or Bad. Your if statement is always true, because when the e is No it is obviously not Bad and you use or (||) operator. So, you are saying: "take this as true if e is not No or it is not Bad.

Use:

if (e != "bad" && e != "No") {

Upvotes: 0

Christian Phillips
Christian Phillips

Reputation: 18749

you need &&, as you're using or (||) above

If e is Bad, then it is not No....if e is No, then it is not Bad...so No or Bad will always filter through.

if (e != "bad" || e != "No") {

should be...

if (e != "bad" && e != "No") {

Upvotes: 2

Roy Dictus
Roy Dictus

Reputation: 33139

Your || should be &&.

You are saying: if the response is not this OR not that -- that is by definition always true.

Upvotes: 10

Related Questions