2017-02-14 57 views
0

我想做一個$ http.post,但這個迴應給我這個錯誤。

XMLHttpRequest無法加載http://XXXXXXXXX:2222/server/api/v1/controllers。預檢響應具有無效的HTTP狀態碼403

所有我讀了互聯網最高審計機關,它的一個故障由CORS,我嘗試沒有成功解決這個問題。

這是我的$ http.post方法。

 // Set the Content-Type 
     $http.defaults.headers.post["Content-Type"] = "application/json"; 

     // Delete the Requested With Header 
     delete $http.defaults.headers.common['X-Requested-With']; 



     var config = { 
      method: 'POST', 
      headers: {'Content-Type': 'application/json; charset=utf-8'} 
     } 

     $http.post("http://XXXXXXXXX:2222/server/api/v1/controllers", objToSave , config) 
      .then(function successCallback(response) { 
      // this callback will be called asynchronously 
      // when the response is available 
      console.log(response) 
      }, function errorCallback(error) { 
      // called asynchronously if an error occurs 
      // or server returns response with an error status. 
      console.log(error) 
      }); 

我正在使用Angular v1.5.5。

希望你能幫幫我,

問候!

+0

CORS錯誤=>檢查CORS設定你的** **的服務器端 – Weedoze

+0

這是否幫助:http://stackoverflow.com/a/38333662/1225328? – sp00m

回答

2

chrome首先發送預檢請求以檢查服務器是否正確響應。如果服務器正確處理該預檢請求,則原始請求將通過chrome發送。嘗試在服務器請求處理程序頭中添加以下行。

Access-Control-Allow-Origin: * 
相關問題