简体   繁体   中英

jquery.ajax json wrong?

hey i have a problem submitting my data via jquery and back:

$.ajax({
    url: "checkAvailability.php",
    type: 'POST',
    data : {data:JSON.stringify(data)},
    success: function(data) {
        if (data.result == 0) {
            alert("0")
        } 
        if(data.result == 1) {
            alert("1")
        }
    }
});

so, ONE of those if-conditions must be true, because of:

checkAvailability.php:

if(isset($_POST['data'])) {
    define('SECURE', true);
    include "storescripts/connect_to_mysql.php";

    require 'AvailabilityChecker.php';

$config = array(etc..);

    $availabilityChecker = new AvailabilityChecker($config);

    $data = $_POST['data'];
    $data = json_decode($data,true);

    preg_match( '/(\d+(\.\d+)?)/', $data['x'] , $m);
    $x =  $m[0];

    if($availabilityChecker->check_availability($x)) {
        echo json_encode(array("error" => "is ok", "result"=>1));
    } else {
        echo json_encode(array("error" => "not ok", "result"=>0));
    }
}

data.result have to be 1 OR 0. anybody can tell me why there is no alert-message? greetings!

UPDATE:

$.ajax({
    url: "checkAvailability.php",
    type: 'POST',
    data : {data:JSON.stringify(data)},
    success: function(data) {
        if (data.result == 0) {
            alert("0")
        } else { alert("fail-1") }
        if(data.result == 1) {
            alert("1")
        } else { alert("fail-2") }
    }
});

now i get first the fail-1 alert and than the fail-2 alert, so both if-conditions are false, why?

You need to specify the dataType, otherwise jquery will instead try to guess what you are trying to do. In this case it is incorrectly guessing text/html rather than application/json.

$.ajax({
    url: "checkAvailability.php",
    type: 'POST',
    dataType: 'json',
    data : {data:JSON.stringify(data)},
    success: function(data) {
        if (data.result == 0) {
            alert("0")
        } else { alert("fail-1") }
        if(data.result == 1) {
            alert("1")
        } else { alert("fail-2") }
    }
});

You should also properly set the content-type header in php, before you echo the json.

header('Content-type: application/json');

You can get away with doing either-or, but i'd suggest doing both.

a solution can be

success: function(d) {

        data = jQuery.parseJSON(d);

        if (data.result == 0) {
            alert("0")
        }
        if(data.result == 1) {
            alert("1")
        }
    }

this becouse $.ajax will no decode the result text from the page for you. what the php code is doin in fact is to print a json string to the stream.

Note that the the output passed to success can be any sort of text (also xml code on simply text)

You need to set the correct content type header in your php file:

header('Content-Type: application/json');
//snip
echo json_encode(array("error" => "is ok", "result"=>1));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM