Sto usando l'API fetch
con React Native.Perché il recupero restituisce un hash strano di numeri interi?
La mia risposta segue un formato normale di {"message": "error here"}
se lo stato è> = 400, che mostrerò in un popup nativo.
Sto cercando di chiamare response.json()
dopo aver rilevato un guasto, ma mantiene mettere tutto in un formato strano ...
{ _45: 0, _81: 0, _65: null, _54: null }
Per qualsiasi motivo ... la risposta effettiva voglio si trova in _65
... Non ho idea di cosa siano queste chiavi casuali.
Quindi attualmente devo accedervi tramite _bodyText
, ma presumo che sia sbagliato perché è un metodo di sottolineatura privato.
Cosa sto sbagliando?
var API = (function() {
var base = 'https://example.com/api/v1';
var defaults = {
credentials: 'same-origin',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
};
var alertFailure = function(response) {
if (response.status >= 200 && response.status < 400) {
return response;
} else {
var json = JSON.parse(response._bodyText || '{}');
var message = json.message || 'There was a problem. Close the app, and try again later.';
var error = new Error(message);
error.response = response;
throw error;
}
};
var callAPI = function(url, opts) {
opts.headers['X-Version'] = 'v' + Package.version;
return fetch(base + url, opts)
.then(alertFailure)
.then((response) => {
return response.json();
})
.catch((error) => {
Alert.alert(null, error.message);
});
};
return {
get: function(url, opts) {
var fullOpts = Object.assign({}, defaults, opts);
return callAPI(url, fullOpts);
},
post: function(url, data, opts) {
var fullOpts = Object.assign({}, defaults, {
method: 'POST',
body: JSON.stringify(data || {})
}, opts);
return callAPI(url, fullOpts);
}
};
})();
Questo è anche lo stesso per 'asyncstorage' – James111
Si è verificato lo stesso problema con' async' e 'await'. Sto ancora cercando di risolverlo. – Dan
Ho aggiunto il mio problema qui http://stackoverflow.com/questions/36285564/why-does-fetch-return-a-weird-hash-of-integers-part-2 – Dan