2017-05-10 24 views
0

我是Angular2的新手。我正在嘗試將ID值綁定到我的網址,但未綁定到網址。我使用的跨域和我的代碼如下:無法將ID值綁定到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

按瀏覽器的政策,CORS是不允許的。即時解決方案使用Chrome擴展[CORS EXTENSION](https://chrome.google.com/webstore/detail/cors-toggle/omcncfnpmcabckcddookmnajignpffnh?utm_source=chrome-ntp-icon)。或 - >設置(訪問控制 - 允許來源)在您的服務器 –

+0

通過瀏覽器熱代碼工作正常? –

+0

so..hot編碼的工作方式...檢查這是post方法或獲取方法 –

回答

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

+0

如果我wana 2通過id 2然後 –

+0

檢查代碼現在 –