user759235
user759235

Reputation: 2207

jQuery ajax issue(succes data not correct)

I am building a small ajax contact form and i am testing it with a basic php file(learning the whole jQuery ajax thing), but for some reason it doesn't works.

Even if the data is correct it stil gives me the error code(if data = ok doent work).

here's the basic jquery code

$(document).ready(function ($) {

    $("#contactform").submit(function(){

        var str = $(this).serialize();

        $.ajax({
            type: obj.attr('method'),
            url: obj.attr('action'),
            data: str,
            dataType: 'html',
            success: function(data){  

                $('.acf-wrap').ajaxComplete(function(event, request, settings){  

                    if(data == 'OK'){
                        msg = 'success';
                    }else{
                        msg = data;

                    }

                    //display msg
                    $(this).html(msg);

                });

            }       
        });
        return false; 
    });
});

test php file

if(1 == 1){
    echo 'OK';
}else{
    echo 'error!!!';
}

Upvotes: 0

Views: 97

Answers (2)

gislikonrad
gislikonrad

Reputation: 3591

The data is probably a JSON string. JSON.parse the data and access the "d" property...

var parsed = JSON.parse(data);
if(parsed.d == 'OK'){
    alert('works');
}

Actually wrote this answer on my phone... Might not be right. Couldn't test it... :)

Upvotes: 0

Matt Stein
Matt Stein

Reputation: 3053

Your PHP will never echo 'OK', because 1 will never equal 2. At least not in the universe I'm from.

if(1 ==2){

How can that ever be true? Either it won't, or a philosopher will need to weigh in.

Upvotes: 1

Related Questions