2

I am going CRAZY posting a json request to a php webservice file when the object I am trying to send is multi-level. ie:

postdata = {
   name:"francesco"
,  age:58
,  address : {
      street:"my Street"
   ,  number: 42
   ,  city:"London"
   }
}

I have tried every example on the web, but, when I read the $_POST data on the php webservice two things happen: if I use JSON.stringify I dont get anything on $_POST or $_GET depending what method I use, and I have to read the file_get_contents('php://input') and then json_decode it (whereas calling the webservce from php I get the info tidily in my $_GET or $_POST globals), If I use other methods I have found, I get the name and age fine, but the address comes through as "[object object]" .

My question is, is it possible, WITHOUT USING jquery, to : - create an object in javascript (multilevel or however the right term) - use the "XMLHttpRequest()" object to post it to the php ws? - read it from php using the $_GET or $_POST globals (depending on method used)?

I have been going crazy for over 96 hours now!!!

Thanks! Francesco

Avi K.
  • 1,734
  • 2
  • 18
  • 28
Francesco
  • 37
  • 8

3 Answers3

0

Question #1

is it possible to create an object in javascript (multilevel or however the right term)

This is how you create an object in javascript:

var o = {
    foo: "bar"
};

Question #2

is it possible to use the "XMLHttpRequest()" object to post it to the php ws?

It's not hard to find it on the web: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest

The code would be something like:

var oReq = new XMLHttpRequest();
oReq.open("POST", "http://www.example.org/target");
oReq.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
oReq.send(JSON.stringify(postdata));

Question #3

is it possible to read it from php using the $_GET or $_POST globals (depending on method used)?

No, you can't. As the docs say, $_POST is:

An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.

So the $_POST is usable only if you're passing form encoded data. Since you're passing a JSON, you're supposed to parse the request body by yourself.

Community
  • 1
  • 1
Alessandro
  • 1,443
  • 9
  • 18
  • I think you answered it in the last part! ... what fooled me was that I was using a routine that I found years ago on the web: – Francesco Mar 21 '16 at 13:21
  • .. which converts a javascript object into a request string, i.e. (using my example) : ?name=francesco&age=58 .. but this obviously fails when it has to face the 2nd level of the object. So how to I write some php code that can handle both proper post requests AND these ones? – Francesco Mar 21 '16 at 13:26
  • @Francesco this is actually another question. I suggest to post it as a separate one – Alessandro Mar 21 '16 at 13:28
  • Thanks I'll try to figure it out by myself, then post if I get stuck! – Francesco Mar 21 '16 at 13:47
0

From what I understand, it is not possible to post data to php from jscipt unless it's a form. but I can do this:

if ($_POST != null)    
  $req = $_POST;
else {
   $json = file_get_contents('php://input');   
   $req = json_decode($json, true);
}

.. and then just read the $req ..

Is this VERY dirty or commonplace??

Francesco
  • 37
  • 8
0

So many incorrect answers here.

  1. To POST a nested object to your PHP script you can use plain js:

    var xhr = new XMLHttpRequest();

    xhr.open("POST", "http://yourwebsite.com/yourscript.php");

    xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");

    xhr.send({"data":postData});

  2. To read the info server-side

    $postData = $_POST['data'];

When I looked to see what PHP had actually given me with error_log(print_r($_POST['test'], true)) I got

Array
(
    [name] => francesco
    [age] => 58
    [address] => Array
        (
            [street] => my Street
            [number] => 42
            [city] => London
        )

)

It's all there.