Pregunta

I have this php-jquery code

<?php
//a php code determines value of the variable $url
echo "<script>
$('#main').html('Loading...'); 
$('#main').load('".$url."');
</script>";
?>

But, when the path indicated in $url doesn't exist, the content of #main remains stuck to "Loading..." and it doesn't display a 404 error. How can I fix this?

¿Fue útil?

Solución

The reason why "Loading" is stuck there is because .load() is an ajax call and although returns a 400 response, does not actually reload the page.

What you can do is make use of jQuery load() callback function like

$("...").load("yourURL", function (responseText, textStatus, req) {
   if (textStatus == "error") {
      //do something here, like redirect the page for example
   }
});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top