簡體   English   中英

使用Amazon Product API進行Angular CORS

[英]Angular CORS with Amazon Product API

創建了一個MEAN堆棧應用程序,我有一個角度組件,在頁面加載時向Amazon Product API發出請求。 我正在localhost(node / express后端)上本地運行和測試。

這是我的組件中的基本請求代碼:

getAmazonTackle(amz:amazonservice) {
var req:Object = {
  method: this.amazonservice.Method,
  url: this.amazonservice.Endpoint, 
  headers: {
    //"Access-Control-Allow-Origin":"*"
  },
  data: this.amazonservice.Data,
  withCredentials: false
}
console.log(JSON.parse(JSON.stringify(req)));
this.$http(req).then(
  function(response) {
    console.dir('The response: '+response);
  },
  function(response) {
    console.dir(response);
    console.dir('The error:'+response);
  }
)

}

當我沒有Access-Control-Allow-Origin標頭時,訪問瀏覽器中的頁面我在Chrome開發控制台中顯示以下內容:

XMLHttpRequest cannot load http://webservices.amazon.com/onca/xml?. 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost:3000' is therefore not allowed access.

當我添加Access-Control-Allow-Origin標頭時,我收到以下內容:

Response to preflight request doesn't pass access control check: 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost:3000' is therefore not allowed access.

我想確定的是,如果我這樣做不正確,或者亞馬遜API不支持CORS。

我遇到了類似的問題,發現如果我創建cors啟用http處理程序,它將作為服務器和亞馬遜服務器之間的中間件。 這樣我們就可以解決這個問題。 創建起來非常簡單,我已經寫好了。

使用真實服務器API開發Angular2應用程序

查看上面帖子中的問題和答案。 類似的代碼我發布在github上,如果需要我會分享位置。

注意:截至目前我處理了get請求,但添加其他方法相當容易。 https://github.com/Anil8753/CORSHttpHandler

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM