Reputation: 1165
I am struggling to understand when does the server return XML
data vs HTML
text data, and how come responseXML
can return both? Is XML
data returned only when making a POST
request?
It's hard to actually test these, because I couldn't properly setup a PHP server, and making a POST
request keeps returning 404 bad request
, but when I make a GET
request, I always get the HTML document
in the responseText
property, but when I try to use responseXML
, I get null
. So, if responseXML
can return either HTML
or XML
, why does it not return the HTML document
then?
Note: Before you accuse me of not doing any research. Let me tell you that I have been doing research for the past 3 days, and the book I'm reading just doesn't clarify these differences, and does not explain what exactly is XML
in the first place. It says that XML
data needs to be parsed to be displayed as a text, but doesn't explain why. It's all very ambiguous. So, I would appreciate if someone could clarify things for me.
Upvotes: 1
Views: 1242
Reputation: 1594
This is an example of getting HTML from server response using fetch API.
fetch('https://someweb.com/api/list').then(function (response) {
// The API call was successful!
return response.text();
}).then(function (html) {
// Convert the HTML string into a document object
var parser = new DOMParser();
var doc = parser.parseFromString(html, 'text/html');
}).catch(function (err) {
// There was an error
console.warn('Something went wrong.', err);
});
Upvotes: 1
Reputation: 2659
POST requests return XML data, if the backend server is configured to return XML data. Completely depends on the server you're talking to, there's no way of predicting the behavior otherwise. Also, it's worth noting that every input can change the behavior of the server. E.g. if you provide a query with a specific value, the server could also return a CSS file, instead of an HTML or XML one.
Upvotes: 3