简体   繁体   中英

How to read the ajax json data in server side

I have created an ajax request in the client side and pass the data to the server side. issue I face is i cannot read the json object passed in the server side. here is the client side code segment I used

var email=document.forms["login-form"]["email"].value;
var password=document.forms["login-form"]["password"].value;

alert("Innn");

$.ajax({

    type:"POST",
    contentType: 'application/json',
    url: "/wcw/wcw/checkuser.php",  
    data: JSON.stringify({email: email ,password: password}),

}).done(function() {

    alert("DONNEEEE");
    window.location.replace("checkuser.php");

});

I wanted to read the json data in the server side.

$input = $_POST["email"];

$result = json_decode($input);

echo $result;

but this will give an error saying

Notice: Undefined index: email in C:\xampp\htdocs\wcw\wcw\checkuser.php on line 18

For fetching JSON object from POST, use php://input stream wrapper . Accessing input stream will allow reading the raw request body.

$result = json_decode(file_get_contents("php://input"));

The error message says it all, it is saying the index email is undefined. Meaning it doesn't exist when you are referencing the index email in $_POST['email'] and that is because of how you set data

$.ajax({
    type:"POST",
    url: "/wcw/wcw/checkuser.php",  
    data: {email: email, password: password},
}).done(function(data){
    alert(data)
})

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