I’m trying to use Ionic 2 build app and do a http post to the server.
The server could receive the post request, and send the return message. However, the param is not passed to the server.
I tried to write a simple POST html page to test the browser and it works after I enable browser’s CROS setting.
Below is my code for the post
import { Injectable, Component } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/Rx';
@Injectable()
@Component({
})
export class UserData{
constructor(public http: Http) {
}
postSetting(uid, setting) {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
let data =JSON.stringify({
uid: uid,
setting: setting
});
return this.http.post('xxx/api/update_user'
, data
, {headers: headers})
.map(res => res.json())
.subscribe(data => {
console.log(JSON.stringify(data));
});
}
I also tried
headers.append('Content-Type', 'application/x-www-form-urlencoded');
...
data = 'uid='+uid+'&setting='+setting;
which is also not working, the http param is still empty.
My questions:
- Will this issue related to the server side? (I could receive the error message from the server saying that param is missing)
- Anyone have some idea about how to solve this issue? Is this related to CORS?
Thanks in advance