CN1002
CN1002

Reputation: 1115

AJAX - Why page redirect is failing here?

I have a problem here. I am posting data using $ajax to update a MySQL table. The update logic is done fine.

PHP Snipet

$count=$stmnt->rowCount();

if ($count==1){
    $output=array('op'=>'tt');
    echo json_encode($output);
}else{
    $output=array('op'=>'ff');
    echo json_encode($output);
}

JS Code

success: function(data) {
                console.log(data);//On update, this is printing{"op":"tt"}

                if (data.op ==='tt') {
                    console.log(data);//this is not executing.
                  window.location.href= 'post.php'
                }else{

                    alert("Error!");
                }
            }

I have realized that my if statement is not being executed. What has gone wrong here?

Upvotes: 0

Views: 29

Answers (2)

fdomn-m
fdomn-m

Reputation: 28621

By default, jquery ajax without a dataType will try to set the response based on the MIME type.

If you have a string, you can manually parse it, ie:

success: function(data) {
    data = $.parseJSON(data);

or you can specify the dataType for jquery to use on the $.ajax request.

Upvotes: 1

Marvinoo_
Marvinoo_

Reputation: 79

You should parse the json first before you can get the plain text out of it.

 var result = jquery.parseJSON(data);
        if (result.op == 'tt') {
            ...
        }

Upvotes: 1

Related Questions