Junior — Middle
How to determine the type or data structure received from the server in the browser?
sobes.tech AI
Answer from AI
In the browser, when receiving data from the server (for example, via fetch or XMLHttpRequest), the type and structure of the data are usually determined by the response header Content-Type and the data format.
Most often, data comes in JSON format. To determine the structure, you can:
- Check the
Content-Typein the response, for exampleapplication/json. - Try parsing the data with
JSON.parse()and see what fields and types are contained. - Use developer tools in the browser (Network tab) to see the raw response and understand the structure.
Example:
fetch('/api/data')
.then(response => {
console.log(response.headers.get('Content-Type'));
return response.json();
})
.then(data => {
console.log(data); // you can examine the structure of the object
});
If the data is not JSON, but, for example, XML or text, you need to use the appropriate parsing methods.