2016-06-09 21 views
0

我需要執行一個get,並得到從控制檯這個錯誤在HTTP請求頭參數

Request header field client_id is not allowed by Access-Control-Allow-Headers in preflight response. 

這是我的頭:

Access-Control-Request-Headers:accept, authorization, client_id 

我想從這個調用的請求頭中刪除client_id,而不會影響全局設置。

下面是代碼:

$http.get(url,{ 
     headers: {"client_id": undefined } 
    }) 
     .success(function (geoData) { 
     d.resolve(geoData); 
     }) 
     .error(function (err, status) { 
     d.reject(err); 
     }); 

但它沒有任何效果。我錯過了什麼?

回答

1

CORS問題引發此錯誤。 Read here有關prelighted請求的更多信息。您需要配置您的服務器來處理餘額請求,以允許客戶頭文件參數,如'client_id'。例如配置價值訪問控制允許報頭服務器

'Access-Control-Allow-Headers' : 'client_id','Content-Type, Authorization, Content-Length, 

對於柄頭一個通過要求一個基礎,你需要定義條件來構建你的頭。它應該是:

// build your header dynamic based on condition of request 
var headerConfig = null; 
if ('your condition to have client_id') { 
    headerConfig = {"client_id": undefined } 
} 
else { 
    headerConfig = {}; 
} 

// request with headerConfig 
$http.get(url,{ 
    headers: headerConfig 
}) 
    .success(function (geoData) { 
    d.resolve(geoData); 
    }) 
    .error(function (err, status) { 
    d.reject(err); 
    });