ajax POST成功结果没有记录

时间:2013-07-25 14:09:24

标签: php ajax jquery

我正在为php页面制作ajax帖子。在php页面上,我回显结果,因此成功回调将记录它,但它不起作用。

JS:

$(function(){

    $.ajax({
        url : "http://XXXXXXX/bn/sample.php",
        type : 'POST',
        number: "1234567",
        success : function (result) {
           console.log("success"); 
           console.log(result);
        },
        error : function () {
           alert("error");
        }
    });

PHP:

<?php

$data = $_POST['number'];

echo json_encode($data);

?>

1 个答案:

答案 0 :(得分:5)

那是因为您将number设置为AJAX json对象中的属性。正确的属性是data

$.ajax({
    url : "http://XXXXXXX/bn/sample.php",
    type : 'POST',
    data: {number: "1234567"},
    success : function (result) {
       console.log("success"); 
       console.log(result);
    },
    error : function () {
       alert("error");
    }
});