fetch
API'yi React Native ile kullanıyorum.Neden getirme, garip bir tamsayı karesi döndürüyor?
Yanıtım, yerel bir açılır pencerede görüntülenecek olan> 400 ise normal bir {"message": "error here"}
biçimidir. sebebi ne olursa olsun ... Ben istediğiniz gerçek yanıtı bulunduğu için
Ben bir hata algılaması sonra response.json()
aramaya çalışıyorum ama garip bir biçimde her şeyi koyarak tutar ...
{ _45: 0, _81: 0, _65: null, _54: null }
_65
'da ... Bu rasgele anahtarların ne olduğu hakkında hiçbir fikrim yok.
Şu anda _bodyText
aracılığıyla erişmeye çalışıyorum, ancak bunun yanlış olduğunu varsayalım çünkü özel bir altçizgi yöntemi.
Neyi yanlış yapıyorum?
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);
}
};
})();
Bu, "asyncstorage" – James111
için de aynıdır. Sadece "async" ve "await" ile aynı sorunla karşılaştım. Hala çözmeye çalışıyor. – Dan
Sorunumu buraya ekledim http://stackoverflow.com/questions/36285564/why-does-fetch-return-a-weird-hash-of-integers-part-2 – Dan