let headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded'
});
let options = new RequestOptions({
headers: headers
});
// TODO: Encode the values using encodeURIComponent().
let body = 'email=' + email + '&password=' + password;
I don’t know, the Http implementation for Angular 2 is still a work in progress, so there could be some changes until the final release. However this is just a utility class, i.e. you could make some helper methods in your service class that could abstract the verbosity. The following article might be helpful:
PHP method POST accept this format ‘Content-Type’ : ‘application/x-www-form-urlencoded’ email=example@gmail.com&password=abcd
solution:
your data like this:
let body = JSON.stringify({ email : “example@gmail.com”, password : “abcd” });
this function convert your output like this : email=example@gmail.com&password=abcd
let postdata = this.formData(body);
The function is a great start to make things a little bit easier.
Essentially use MPUNIA code above but don’t JSON.stringify your object first. Just toss in the object to the function like below.
//Example usage of below function
let body = this.jsonToURLEncoded({ username: usrnme, password: psswrd });
//convert a json object to the url encoded format of key=value&anotherkye=anothervalue
private jsonToURLEncoded(jsonString){
return Object.keys(jsonString).map(function(key){
return encodeURIComponent(key) + '=' + encodeURIComponent(jsonString[key]);
}).join('&');
}
Full credit to MPUNiA. Just renamed the function to something I thought was a little more legible.
Thank you for all your solutions so far!
Unfortunately there is still nothing ending up in my database. Can please provide someone the PHP-part to fetch those post-Data?
Hi, i also faced the same problem sometime when I was trying to send data to the serve, here is what I did now it works fine.
step 1:
create a formData object and append the data you want to send on it.
let body = new FormData();
body.append("key ", "value ");
// the key is what you will reference in your server code ie
$name = $this->input->post(“key”); if using codeigniter ot $_POST[“key”];
step 2:
Set your headers as well
let headers = new Headers(); // import the headers from the '@angular/http’
headers.append(“Content-Type”, “application/json”);
step3:
Make sure you subscribe to your post request.
step 4:
Server side make sure to add the the access control allow origin headers. like this
header(“Access-Control-Allow-Origin: *”);
Galera, estou com um problema no envio de post: Sempre que vou enviar um post:
let headers = new Headers({
‘NDAPI-Key’: ‘XXXXXXXXX’,
‘NDAPI-Host’: ‘XXXXXXXXX’
});
let options = new RequestOptions({ headers: headers });
esse é o meu metodo post para enviar os dados, mas quando chega no webservice rest para ele realizar a conversão:
public function create(Request $request) {
return json_decode($request->input(‘cliente’));
}
ele apresenta um erro interno, mas quando envio os json pela aplicação android mesmo nativa utilizando o retrofit ele recebe e converte normal sem problemas.
Gostaria de saber se alguém já passou pela mesma situação, estou há muito tempo tentando resolver isso… Obrigado! Sei que não é em inglês a pergunta mas se alguém pudesse ajudar ficaria muito grato mesmo.