Spilot
Spilot

Reputation: 1525

ajax POST success result not logging

I'm making an ajax post to a php page. On the php page I echo the result so the success call back will log it, but it doesn't work.

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);

?>

Upvotes: 0

Views: 242

Answers (1)

Prisoner
Prisoner

Reputation: 27618

That's because you're setting number as a attribute in your AJAX json object. The correct attribute is 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");
    }
});

Upvotes: 5

Related Questions