2
Hello,
I have a requisition GET
in Jquery
, to load a page HTML
; and if the page does not exist, then displays in the container a <div>erro 404</div
.
The Jquery request works well.
Jquery request
const loadHtml = (url, element) => {
$.get(url, function(response) {
$(element).html(response);
}).fail(function() {
$(element).html('<div>erro 404</div>');
})
}
However I want to remove Jquery from the application and in this case I want to use javascript puro
, so I thought I’d use Promise
.
It turns out that when the página HTML
Does not exist, the message displayed in the container is not <div>erro 404</div>
, and yes to server message 404.
Can help me better the code below so that if the page does not exist I can display the <div>erro 404</div>
?
Promise
const loadHtml = (url, element) => {
fetch(url, {
method: 'get' // opcional
})
.then(function(response) {
response.text()
.then(function(result) {
console.log(result);
$(element).html(result);
})
})
.catch(function(err) {
$(element).html('<div>erro 404</div>');
});
}
But in this case the return is being a
json
, in the example I passed I am requesting an HTML page. Example:loadHtml('pages/page.html', elemento)
?– Wagner Fillio
@Wagnerfillio the same way ... the result is different but the way to get to it is the same
– novic