2016-02-22 30 views
7

我在一起使用Cordova和提取API時遇到問題。 當代碼中的「X-的authToken」報頭被正確地檢索並記錄該瀏覽器執行我執行以下代碼提取API和科爾多瓦

fetch(BASE_URL + '/auth/login', { 
    method: 'post', 
    credentials: 'include', 
    headers: { 
    'Accept': 'application/json', 
    'Content-Type': 'application/x-www-form-urlencoded' 
    }, 
    body: transformRequest({username: email, password: password}) 
}).then(response => { 
     console.log(response.headers.get('X-AuthToken')) 
}); 

。當我在我的Cordova應用程序中打包時運行相同的代碼時,'X-AuthToken'標頭爲空。 此外奇怪的是,當檢查響應服務器端和在網絡上嗅探時,我可以完美地看到標題集,因此我完全確定標題在那裏(簡單地說它不是由獲取API返回的)。事實上,當使用等效XMLHttpRqeuest頭設置正確:

var xhttp = new XMLHttpRequest(); 
xhttp.open("POST", BASE_URL + /api/auth/login", true); 
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
xhttp.send("username=username&password=password"); 
xhttp.onreadystatechange = function() { 
    if (xhttp.readyState == 4 && xhttp.status == 200) { 
     console.log (xhttp.getResponseHeader('X-AuthToken')); 
    } 
} 

值得一信號,當我試圖轉儲像編譯,緩存控制等常見的標題,...他們是正確的方式記錄。 它像抓取API的接口正在過濾標題並刪除那些不是標準的。 是否有人遇到同樣的問題?我錯過了什麼嗎?

+1

fetch api在移動瀏覽器上沒有很好的支持。 http://caniuse.com/#feat=fetch – albanx

+1

@pinturic android和ios cordova項目都使用webkit。 Webkit尚未100%支持提取API。請參閱下面的答案 –

回答

1

很好地提出了問題,你正在發展的前沿。現在我會堅持使用XMLHTTPRequest。抓取api在webkit中沒有正確實現。請參閱webkit bugzilla bug 151937