简体   繁体   中英

$.ajax not POSTing my variable?

var code = $.cookie('code');
$.ajax({
    type: "POST",
    url: "ajax/loadData.php",
    data: { 'code': code },
    dataType: "json",
    success: function(data) {
        console.log(data.coins);
    },
    error: function(emsg) {
        alert(emsg.responseText);
    }
});

Basically it shows the response text and within it, it has: Notice: Undefined index: code in D:\\wamp\\www\\ajax\\loadData.php on line 3 and I'm unsure why this is so, so if anyone could help me clear it up, I'd be grateful.

loadData.php:

<?php
include '../inc/_db.php';
$code = $_POST['code'];
$query = mysqli_query($db, "SELECT * FROM data WHERE code='$code'");
$rows = array();
while($row = mysqli_fetch_array($query)) {
    $rows[] = $row;
}
echo json_encode($rows);
?>

Try setting the data parameter to proper json

$.ajax({
    type: "POST",
    url: "ajax/loadData.php",
    data: { 'code': code  },
    dataType: "json",
    success: function(data) {
        console.log(data);
    },
    error: function(emsg) {
        alert(emsg.responseText);
    }
});

The error is stating that $_POST['code'] is not set, therefor the first place to look is where you set it.

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