无法将ID值绑定到URL

时间:2017-05-10 07:01:13

标签: angular angular2-services

我是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时,它的效果非常好。

1 个答案:

答案 0 :(得分:0)

Angular POST方法

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

的角度应用