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?