2017-04-14 52 views
2

林400錯誤的請求試圖從角2與角2

調用的Magento 2的REST API從很長的面對這個問題,確實需要在同一個或其中ATLEAST建議的修復程序獲取JSON響應問題是?

下面是如何從即時調用Angualar REST:

@Injectable() 
export class ProductService { 
public _productUrl = 'http://10..../Mage_ang2/index.php/rest/V1/customers/1'; 

constructor(private _http: Http) { } 

getProducts(): Observable<IProduct[]> { 
let headers = new Headers({ 'Content-Type': 'application/json; charset=utf-8' }); 
headers.append('Authorization', 'Bearer ntthnrbj1uam2tuv1ekva7n8jh18mcnkby3'); 
let options = new RequestOptions({method: RequestMethod.Get, headers: headers }); 


console.log(headers); 
    return this._http.get(this._productUrl,options) 
     .map((response: Response) => <IProduct[]> response.json()) 
     .do(data => console.log('All: ' + JSON.stringify(data))) 
     .catch(this.handleError); 
} 

當我張貼在郵差一樣,我得到迴應。

當我通過Angular2運行,使用相同的頭文件時,我得到一個JSON響應但有400(錯誤請求)!

此外,響應並不是真正的請求調用。

enter image description here

enter image description here

+0

在瀏覽器中的控制檯說什麼? – Aravind

+0

我附上了瀏覽器控制檯的屏幕截圖,其中說,400,但我得到一個JSON數據 – Sach

+0

已更新錯誤,即時通訊在控制檯@Aravind獲得...已做足夠的搜索這個重新提取和CORS問題,需要幫助確定問題在哪裏? – Sach

回答

0

試試這個,它應該工作

let headers = new Headers({ 
     'Content-Type': 'application/json', 
     'Authorization':'Bearer ntthnrbj1uam2tuv1ekva7n8jh18mcnkby3' 
    }); 

return this._http.get(url,{headers:headers}) 
     .map(response: any => { 
      return response.json(); 
     }); 
+0

你能告訴我的代碼和我的變化是什麼!你在哪裏使用get()? – Sach

+0

@對不對,有'get()'。您可以通過比較請求頭來看到差異。我還沒有試過 – Thabung

+0

@danimal:感謝編輯建議 – Thabung

1

我一直在使用相同的語法,在我的示例應用程序,但沒有授權頭。一切似乎都正確。

問題可能在於設置授權標頭。檢查授權令牌的正確性。

試試這個語法設置頁眉

let headers = new Headers({ 'Content-Type': 'application/json; charset=utf-8';Authorization: **token** });