Can not compare the answer AJAX jquery PHP

1

I hope you can help me. I am working on a web project in php and ajax of JQUERY the problem I have is in the following code:

$.ajax({
        url: ruta,
        type: 'POST',
        data: img,
        contentType: false,
        processData: false,
    })
    .done(function(res) {


            if(res==="a"){
                $("#error-img").html("Error, el archivo no es una imagen");
                $("#error-img").addClass('text-danger');

            }else if(res==="b"){
                $("#error-img").html("Error, el tamaño máximo permitido es un 1MB");
                $("#error-img").addClass('text-danger');

            }else if(res==="c"){
                $("#error-img").html("Error, el tamaño máximo permitido es un 1MB");
                $("#error-img").addClass('text-danger');

            }else if(res==="d"){
                $("#error-img").html("Error la anchura y la altura maxima permitida es 500px");
                $("#error-img").addClass('text-danger');    

            }else if(res==="e"){
                $("#error-img").html("Error la anchura y la altura mínima permitida es 60px");
                $("#error-img").addClass('text-danger');

            }else{

                $("#error-img").html("Imagen subida con exito");
                $("#error-img").addClass('text-success');
            }



    });

The php file returns a string to me, what I want is to compare it by means of if () but it does not do it, it only launches the else and it does not compare, someone can tell me why I can not. I HAVE A SUSPICION THAT MAY BE FOR THIS:

        contentType: false,
        processData: false,

Please, I hope your quick help I'm going crazy

    
asked by Arellano Cornejo Daniel 02.01.2018 в 21:47
source

1 answer

1

The contentType: false should not be a problem.

If you want, you can add dataType: "html" to the Ajax request.

Regarding the comparisons, the value may be coming from the server with leftover spaces. Then clean the variable with trim .

I take this opportunity to improve the code a bit:

$.ajax({
    url: ruta,
    type: 'POST',
    data: img,
    dataType: 'html',
    contentType: false,
    processData: false,
})
.done(function(res) {

        var res=res.trim();
        var txtClase='text-danger';
        var txtHtml="";

        switch (res) {
            case "a":
                        txtHtml='Error, el archivo no es una imagen';
                        break;
            case "b":
                        txtHtml='Error, el tamaño máximo permitido es un 1MB';
                        break;
            case "c":
                        txtHtml='Error, el tamaño máximo permitido es un 1MB';
                        break;

            case "d":
                        txtHtml='Error la anchura y la altura maxima permitida es 500px';
                        break;

            case "e":
                        txtHtml='Error la anchura y la altura mínima permitida es 60px';
                        break;

            default:
                        txtHtml='Imagen subida con exito';
                        txtClase='text-success';
        }

        $("#error-img").html(txtHtml);
        $("#error-img").addClass(txtClase);

});
    
answered by 02.01.2018 / 22:38
source