0

I have this html / javascript :

<html>
    <head>
    </head>
    <body>
        <script src="view/backoffice/assets/plugins/jquery/jquery-1.11.1.min.js" type="text/javascript"></script>
        <script>
            $(document).ready(function(){
                var data = {'user_id':'2680',
                            'ship_to_name':'John Doe',
                            'ship_to_address':'Somewhere in Jawa Timur',
                            'ship_to_city':'Surabaya',
                            'ship_to_area':'Wonocolo',
                            'ship_to_phone':'080000000'};

                $.ajax({
                    url: "../controller/ctrl.php",
                    type: 'POST',
                    contentType:'application/json',
                    data: JSON.stringify(data),
                    dataType:'json'
                });
            });
        </script>
    </body>
</html>

and I have this PHP on my server :

<?php
    $jsonReceiveData = json_encode($_POST);
    file_put_contents('storedvar.php', $jsonReceiveData);
?>

but what I got in storedvar.php is just empty variable of json. just like this [].

why I can't get json sent data? thank you.

Saint Robson
  • 5,475
  • 18
  • 71
  • 118
  • 1
    Does the answer here solve your problems? http://stackoverflow.com/questions/18866571/receive-json-post-with-php – epascarello Apr 22 '16 at 12:13
  • simply remove the single quotes from all keys of data object. then remove stringify as well. use this format shows in jquery doc { name: "John", time: "2pm" } – Syed Ekram Uddin Apr 22 '16 at 12:15
  • May you can try by removing the 'contentType' attribute ? may it will help. – Alankar More Apr 22 '16 at 12:16
  • @SyedEkramUddinEmon — That won't help at all. Using identifiers instead of strings for property names in a JavaScript object literal will have exactly the same result. – Quentin Apr 22 '16 at 12:17
  • @AlankarMore — Then the code will still be sending JSON, it just won't be telling the server that it is JSON. PHP still won't be able to populate `$_POST` with it. – Quentin Apr 22 '16 at 12:17
  • The question is closed. If you want to answer it then either answer the duplicate or (if you think the close reason is wrong) vote for it to be reopened. Please stop putting answers it in the comments. – Quentin Apr 22 '16 at 12:18
  • Update ajax call: $.ajax({ url: "ctrl.php", type: "POST", data: data, }); In ctrl.php: var_dump($_POST); You will get regular $_POST array; ... – salih0vicX Apr 22 '16 at 12:18
  • @SyedEkramUddinEmon Do you realize that objects in JavaScript can use double quotes, single quotes, or unquoted. They are all exactly the same... There are reasons on why you should use quotes, but I am not going to go into details. – epascarello Apr 22 '16 at 12:27

1 Answers1

-1

your data is json , not a string.

replace it like this but dont split the lines.

 var data = "{'user_id':'2680',"+
                            "'ship_to_name':'John Doe',"+
                            "'ship_to_address':'Somewhere in Jawa Timur',"+
                            "'ship_to_city':'Surabaya',"+
                            "'ship_to_area':'Wonocolo',"+
                            "'ship_to_phone':'080000000'}";

if that works you might wish to remove the stringify and see if the original json works.

Stelium
  • 1,207
  • 1
  • 12
  • 23
  • 1
    removing `stringify` or not, your answer gives me this : `Uncaught SyntaxError: Unexpected token ILLEGAL` in console. – Saint Robson Apr 22 '16 at 12:05
  • answer makes no sense, plus when you do that, that string is not valid JSON and it contains line breaks so it is not even a valid JavaScript. How this got upvoted makes me scratch my head. – epascarello Apr 22 '16 at 12:07
  • @epascarello : I tried this too `var data = {'user_id':'2680','ship_to_name':'Robert Hanson','ship_to_address':'Somewhere in Jawa Timur','ship_to_city':'Surabaya','ship_to_area':'Wonocolo','ship_to_phone':'080000000'};` but it gives me empty result too... – Saint Robson Apr 22 '16 at 12:08
  • @epascarello : the data I just showed you is without line breaks. – Saint Robson Apr 22 '16 at 12:09
  • 1
    @RobertHanson My comment was about the answer... The answer is wrong. – epascarello Apr 22 '16 at 12:11
  • simply remove the single quotes from all keys of data object. then remove stringify as well. use this format shows in jquery doc { name: "John", time: "2pm" } – Syed Ekram Uddin Apr 22 '16 at 12:11
  • 1
    There is nothing wrong with the object.... ahh – epascarello Apr 22 '16 at 12:12
  • 1
    @sayed Removing single quote will not help. JSON can have single quote for keys no problem with that. – Alankar More Apr 22 '16 at 12:12
  • @AlankarMore — No it can't. JSON only allows double quotes to delimit strings. (JavaScript string literals can use `'` or `"` but JavaScript isn't JSON) – Quentin Apr 22 '16 at 12:14
  • Things wrong with this answer: (1) JavaScript string literals can't include literal new lines. (2) JSON strings must be delimited with `"` not `'`. (3) The JavaScript string will be stringified to JSON by `JSON.stringify()` which double encodes it. (4) The PHP is unchanged so it will still get nothing because `$_POST` isn't populated. – Quentin Apr 22 '16 at 12:15
  • there is not problem with using single quotes for json. It is against the rules but there is no problem with it – Stelium Apr 22 '16 at 15:15
  • you could have edited the line split, that was an obvious error but the answer still works. – Stelium Apr 22 '16 at 15:17