içinde bir json nesnesi olup olmadığını nasıl kontrol ederim Bir JSON veya bir URL'den metin almak için fetch polyfill kullanıyorum, yanıtın bir JSON nesnesi olup olmadığını nasıl kontrol edebilirim? Bir getirme yanıtının javascript
fetch(myRequest).then(response => {
const contentType = response.headers.get("content-type");
if (contentType && contentType.indexOf("application/json") !== -1) {
return response.json().then(data => {
// process your JSON data further
});
} else {
return response.text().then(text => {
// this is text, do something with it
});
}
});
içeriğin geçerli JSON olduğundan kesinlikle emin olmak gerekiyorsa
(ve don': sadece metinfetch(URL, options).then(response => {
// how to check if response has a body of type json?
if (response.isJson()) return response.json();
});
http: // stackoverflow com/a/20392392/402037 – Andreas