I am trying to send json with ajax to php file for that I have tried below code
-
Using jquery
var dummyData = {'bob': 'foo', 'paul': 'dog'}; var ajaxRequest = $.ajax({ url: "json_handler.php", type: "POST", contentType: "application/json; charset=utf-8", data: JSON.stringify(dummyData ), dataType: "json" }); ajaxRequest.done(function (response, textStatus, jqXHR) { console.log(response + textStatus + jqXHR); alert('sd'); }); ajaxRequest.fail(function (e) { console.log(e); });
And i am just doing var_dump($_REQUEST)
at json_handler.php
and ajax request is keep failing and get nothing in response text
When i tried
$json = file_get_contents('php://input');
var_dump($json);
I got the json but ajax request is still failing.
I have also tried to do this javascript and my code is below
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = stateHandler;
httpRequest.open("POST", "json_handler.php", true);
httpRequest.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
httpRequest.send( JSON.stringify(dummyData ));
Here is my console log