-1

I'm sending JSON to backend server, but I'm confuse about how I can process it correctly, i'm reading around that echo (json_decode($_POST)); is not going to work but echo (json_decode(file_get_contents("php://input")));, but actually I tried to output the response on the client side alert(respon); but nothing show

0_12_e2_contentType_JSON.html

<html>
<head>
    <script type="text/javascript" src="jquery-2.0.3.min.js"></script>
</head>
<body>
<script >

    $(function(){
        $.ajax({
            url: "fragment/0_12_e2_contentType_JSON.php",
            type: "POST",
            contentType:"application/json; charset=utf-8",

            data: {
                "name":"hans" ,
                "id":10
            },
            success: function (respon)
            {
               alert(respon);
            },
            error:function(e){
            alert('Ajaxnya error');
               }
        });
  });
</script>
</body>
</html>

0_12_e2_contentType_JSON.php

<?php
echo (json_decode(file_get_contents("php://input")));
?>

My question is that is it already correct? but why does it output nothing? Thanks

Mihriban Minaz
  • 3,043
  • 2
  • 32
  • 52

2 Answers2

1

If you want to send contentType:'application/json' you need to stringify the data yourself

 $.ajax({
        url: "fragment/0_12_e2_contentType_JSON.php",
        type: "POST",
        contentType:"application/json; charset=utf-8",

        data: JSON.stringify({ "name":"hans" ,"id":10}),
        success: function (respon)
        {
           alert(respon);
        },
        error:function(e){
        alert('Ajaxnya error');
           }
    });

Most people would not override the default contentType and receive in php using $_POST

charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • $_POST won't work with me (somehow it *will* has to do with the `contentType`) – Plain_Dude_Sleeping_Alone Feb 01 '16 at 14:34
  • in fact ...you can send it as `data:data: { "name":"hans" ,"id":10}` and then use `json_encode($_POST)` on srv – fedeghe Feb 01 '16 at 14:35
  • @fedeghe not if you override default contentType – charlietfl Feb 01 '16 at 14:35
  • Maybe *contentType* just means to be indicating the type of request body I sent, but not converting the data that I sent . Well I'm a bit new to JQuery :D , thanks Anyone . @fedeghe, I will give you upvote if you change your answer, coz I'm sending JSON *not* to receive it :D – Plain_Dude_Sleeping_Alone Feb 01 '16 at 14:38
  • @Bravo you are making it more complicated than it needs to be by adding your own contentType. See my update at bottom of answer. Read the docs to understand difference between `dataType` and `contentType` – charlietfl Feb 01 '16 at 14:39
  • dear @Bravo... thank You for the undue explanation... I wont sell it for an upvote, I really do not care, has no value ... You're sending a literal, the easiest way is to use a framework as u do, and I't so funny to see how such a thing can cause problems. – fedeghe Feb 01 '16 at 14:45
  • @fedeghe, If I know that will cause a problem then why would I ask that anyway? you should already know that when I said `contentType` i'm sending to server, if wrote `dataType` i'm receiving from the server :) btw don't cry hehe, I will give +10 to your another post . Not here (coz I downvote it, sorry) – Plain_Dude_Sleeping_Alone Feb 01 '16 at 14:47
  • Thanks for your helps everyone, I'm going to logout,. – Plain_Dude_Sleeping_Alone Feb 01 '16 at 14:50
  • @Bravo again strange way, from cli is not mandatory and usually is done via header in php ....anyway, bravo! You got it! :D – fedeghe Feb 01 '16 at 14:53
-2

not clear to me Your question anyway it seems like in the end You want to answer from the server what You sended via ajax... if it is the case I would recommend to use json_encode ... not json_decode

<?php
header('Content-Type: application/json');
echo (json_encode($_POST));
?>
fedeghe
  • 1,243
  • 13
  • 22
  • I mean I'm sending JSON :D – Plain_Dude_Sleeping_Alone Feb 01 '16 at 14:24
  • `json_encode()` is wrong ... `file_get_contents()` returns string – charlietfl Feb 01 '16 at 14:25
  • really? ... so You're missing something cause json_decode takes a string ..can't take an array (I'm looking at Your question) btw... http://stackoverflow.com/questions/4064444/returning-json-from-a-php-script so anyway You should use the right header before the response `header('Content-Type: application/json');` – fedeghe Feb 01 '16 at 14:27
  • I'll downvote Your question cause in the end You accepted an answer that IS NOT `How to send JSON in php via jquery correctly`. Maybe no one will ever rely on it – fedeghe Feb 01 '16 at 15:35
  • well at least you told me that you were the one downvoted me *:D* hahahah, no problem mate! please just don't take it personally ;) – Plain_Dude_Sleeping_Alone Feb 01 '16 at 23:04