2017-10-08 50 views
0

我已經導入了標題,並且包含了對象,url和標題。但它的示值誤差類型'標題'與'RequestOptionsArgs'類型沒有共同的屬性

類型「頭」沒有共同的屬性類型「RequestOptionsArgs」

我的代碼:

getContactData(aplhaNumericId:any) { 
    let obj = {aplhaNumericId:aplhaNumericId}; 
    let headers=new Headers(); 
    headers.append('Content-Type','application/json; charset=utf=8'); 
    return this.http.post('http://ip/ex/api/api/JsonServices.php',JSON.stringify(obj),headers) 
    .map((res:Response) => res.json()); 
} 
+0

PLS何況你的角度版本,給我們一點信息。 – asmmahmud

+0

當然,我正在使用角度4 + –

+0

角度4.3 +或以下?你使用'@ angular/common/http'中的HttpClient;或舊的'@角/ http'? – asmmahmud

回答

1

您需要在RequestOptions通過Headers傳遞之前,到post(...)

return this.http.post('http://ip/ex/api/api/JsonServices.php',JSON.stringify(obj), 
    new RequestOptions(headers: headers)) 

+0

我已經更新了我的代碼,但它顯示如下錯誤:「跨源請求被阻止:同源策略不允許(原因:缺少CORS頭'Access-Control-Allow-Origin') 錯誤 對象{_body:error,status:0,ok:false,statusText:「」,headers:{... },輸入:3,url:null} –

+0

以下是m代碼: –

+0

postContactUsData(){ let obj = {id:'78dfak98s'}; let headers = new Headers({'Content-Type':'application/json','Access-Control-Allow-Origin':'*'}); let options = new RequestOptions({headers:headers}); return this.http.post('url',JSON.stringify(obj),options) .map((res:Response)=> res.json()); } –

0

頭只的HTTP請求的選項之一。嘗試使用RequestOptions

import { RequestOptions } from '@angular/router'; 

getContactData(aplhaNumericId:any){ 
    let obj = {aplhaNumericId:aplhaNumericId}; 
    let headers=new Headers(); 
    headers.append('Content-Type','application/json; charset=utf=8'); 
    // add this 
    let options = new RequestOptions({headers: headers}); 
enter code here 
    return this.http.post('http://ip/ex/api/api/JsonServices.php', JSON.stringify(obj), options)) 
     .map((res:Response) => res.json()); 
} 

注意這是舊的HttpModule

相關問題