1

I'm having trouble making POST requests to a server, my data is not send back to me.

I have a very simple PHP script:

Server script

<?php
   header('Access-Control-Allow-Origin: *');
   header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");

   echo json_encode("{user-id:" . $_POST["user_id"] . "}");
 ?> 

I want to make a POST request to this script, and get some JSON data as response.


Working request

If I make a POST request from HTML, this works perfectly:

<form method="post" action="http://url/post.php">
  <input type="hidden" name="user_id" value="123" />
  <button>Go to user 123</button>
</form>

This also works:

$.post( "url/post.php", this.data)
  .done(function( data ) {
    console.log( data );
  });

Response

"{user-id:123}"

 


Not working script (desire): Aurelia Fetch Client (JS2016)

submit(){
    let comment = { user_id: "234" };
    this.http.fetch('post.php', {
      method: 'post',
      body: json(comment)
    })
      .then(response => response.json())
      .then(data => console.log(data));
}

Fetch client configuration

config
  .useStandardConfiguration()
  .withBaseUrl('url')
  .withDefaults({
    mode: 'cors',
    headers: {
      'Accept': 'application/json'
    }
  });
});

Response

{user-id:}

php alternative

I've tried to see what's inside the array in PHP, but I got the same result using implode:

echo json_encode("{user-id:" . (string)implode(" ",$_POST) . "}");

The question

It looks like Aurelia does not post the data the right way. Do I make a mistake here, or is it some configuration setting I'm not aware of?

Randy
  • 9,419
  • 5
  • 39
  • 56
  • What is that `json` function? Notice that your original form does post a `application/x-www-form-urlencoded` body, not json. And your PHP routine to encode JSON is horrible - if you want a JSON object, you shouldn't pass a string to `json_encode` – Bergi Jun 07 '16 at 14:58
  • Could you change `body: json(comment)` to `body: JSON.stringify(comment)` and see what happens? – DavidDomain Jun 07 '16 at 15:02
  • @Bergi That `json` function is one of the Aurelia recommendations. Removing that and passing valid JSON didn't do the trick, nor did strings. As for the PHP; I'm not a PHP programmer but I wanted to see if my `POST` was fired the right way. The PHP did return JSON, whenever I returned a normal string or something else, the `http-fetch-client` gave an error. – Randy Jun 07 '16 at 15:11
  • The JSON that your PHP should return would like this: `{"user-id":"123"}` – Bergi Jun 07 '16 at 15:12
  • @Bergi If I change the `http-fetch-client` to `$.post()` it does work, doesn't that mean the PHP is fine? I did change it to: `echo json_encode($_POST);` wich turns the `$_POST` array into json. – Randy Jun 07 '16 at 15:14
  • @randy, not really. Aurelia-Fetch-Client uses `fetch` while `$.post()` uses `XmlHttpRequest`. They are different ways to make an "ajax call". The problem is that your json not well-formed. The response should be something like `{"user-id":""}` – Fabio Jun 07 '16 at 17:14
  • You are sending the JSON as part of the body and the PHP is trying to read the _POST parameters (they are two different things). You should read the content of the body on the PHP side with `file_get_contents('php://input');` or change the Aurelia code to send it as POST parameters. – Marc Compte Jun 07 '16 at 17:30

1 Answers1

2

Instead of "_POST["user_I'd"]" do this

$input = file_get_contents('php://input');
$input = json_decode($input);
//access user_id like this:
$input->{'user_id'}
Gilbert Nwaiwu
  • 687
  • 2
  • 13
  • 37