我是Angular2
的新手。我正在尝试将ID
值绑定到我的URL,但它没有与URL绑定。我使用跨域,我的代码如下:
onselect(id: number) {
let data = JSON.stringify(id);
let headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded'
});
let options = new RequestOptions({
headers: headers
});
var GetstateValue = this.http.post("http://localhost:34339/Home/GetStateById/?id", id, options)
GetstateValue.subscribe((res => this.Success(res)), res => this.Error(res));
console.log(GetstateValue, data);
}
当我将我的网址硬编码为http://localhost:34339/Home/GetStateById/1
时,它的效果非常好。
答案 0 :(得分:0)
Angular HTTP post method need header and pass data in JSON data.
onselect(id: number) {
let data = JSON.stringify(id);
let headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded'
});
let options = new RequestOptions({
headers: headers
});
let id = id;
var GetstateValue = this.http.post("http://localhost:34339/Home/GetStateById", id, options)
GetstateValue.subscribe(
res => {
this.result = res;
console.log(this.result);
},
res => {
this.Error = res;
console.log(this.error);
}
);

请参阅我的github链接HTTP service 请参阅http post方法link
的角度应用