Fetch API and Cordova

I'm having trouble sharing the Cordova and Fetch APIs. I am executing the following code

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')) }); 

When the code is executed in the browser, the "X-AuthToken" header is correctly loaded and registered. When I run the same code when it is packaged in my Cordova application, the β€œX-AuthToken” header is NULL. Moreover, it is strange that I can clearly see the set of headers when checking the side of the response server and when sniffing on the network, so I am absolutely sure that there is a header (it just does not return by the API); in fact, when using the equivalent XMLHttpRqeuest, the header is correctly set:

 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')); } } 

It is worth signaling that when I try to reset other common headers, such as pragma, cache control ... they are logged correctly. It seams, like the Fetch API, filters headers and deletes those that are not standard. Is someone else experiencing the same problem? Am I missing something?

+7
javascript cordova
source share
1 answer

The positive question is, you are developing to the cutting edge. For now, I would stick with XMLHTTPRequest. Fetch api is not implemented in webkit. See bugzilla website error 151937

+1
source share

All Articles