3

I am trying to send json with ajax to php file for that I have tried below code

  1. 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

enter image description here

Richerd fuld
  • 364
  • 1
  • 10
  • 23

2 Answers2

4

See because your ajax is having dataType: "json" so it is expecting the response in json format which is like {key:value} or [{k:v}, {k:v}, ....] and you are not echoing any json structure so this is where your ajax is failing.

Seems to me you have to write it this way:

$json = file_get_contents('php://input');
echo json_encode($json);
Jai
  • 74,255
  • 12
  • 74
  • 103
  • thanks its working fine now , but why i have to use file_get_contents rather than just $_REQUEST – Richerd fuld Aug 24 '15 at 09:07
  • That is because you have changed the headers in the ajax code `contentType: "application/json; charset=utf-8",` if you do this then you have to use `file_get_contents` because php natively support `formurlencoded` query strings. [This will help you understanding it.](http://stackoverflow.com/a/8893792/1059101) – Jai Aug 24 '15 at 09:09
  • so you mean if i dont use `application/json;` header and as i am stringfy json it should work – Richerd fuld Aug 24 '15 at 09:11
  • if you remove `application/json` then you don't have to strigify either ajax just needs an object to be passed `data:{k:v}` would be enough. – Jai Aug 24 '15 at 09:13
  • i have tried `$.ajax({ url: "ajax.php", type: "POST", data: JSON.stringify(serData), });` now i am not getting data at server but when i get data by `file_get_contents` its working – Richerd fuld Aug 24 '15 at 09:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87754/discussion-between-richerd-fuld-and-jai). – Richerd fuld Aug 24 '15 at 09:17
  • @Richerdfuld and at PHP end you need to decode it before `json_decode`. – Jai Aug 24 '15 at 09:18
0

Your request seems good, but why do you stringify your JSON?

You can post payload data:

data: dummyData

To debug and test your json post, you can use extensions as Postman or Advanced Rest Client.

schellingerht
  • 5,726
  • 2
  • 28
  • 56