Handle get(403,404,405) errors with jquery

Asked

Viewed 1,138 times

1

I’m using history API to load the clicks in the menu, in a div #content, ok so far so good, but I’m handling the errors by JQUERY even look only

$('a').click(function() {
    pageurl = $(this).attr('href');
    if(pageurl!=window.location){
        window.history.pushState({path:pageurl},'',pageurl);
        }
     $('#conteudo').load($(this).attr('href'), function(responseText, statusText, xhr)
                {

         if(xhr.status == 403) { 
             $('#conteudo').load("/error/error403");

         }
         if(xhr.status == 404) { 
             $('#conteudo').load("/error/error404");

         }
         if(xhr.status == 405) { 
             $('#conteudo').load("/error/error405");

         }

 });

      return false;
    });

I would like to know if this is really feasible, since I am using VRAPTOR and I can only imagine this way of treating errors in a "efficient" way someone there had problems or can see a possible problem?

if this is relevant, if the controller does not exist it sends the non-existent page error(404), if the user tries to access an unauthorized method, returns the 403(Forbidden) and so on..

EDITED FOR question 2:

well, I have the following structure

I have index.php which contains 2 Divs

<div id="menu"> 
e <div id="conteudo">

when you load the index, it pulls . load(/test/menu) to the div=menu

and when I click on any link in the menu, it triggers the function giving $("#content"). load(/test/Linkclicked)

works perfectly, only that if I type in the browsed /test/Linkclicked or /test/menu, it will simply open only the respective ones without the correct division, the linkClicate will open only the html referring to it without the MENU above... and so if I open the link of the /test/menu, it will show only the menu without the contents

how do I so that when I open the /test/Linkclicked it opens the COMPLETE page that would be the /test/index, but in the position referring to the link typed in the case /test/Linkclicked

1 answer

1


According to the documentation of jQuery.load(), if you compare the return parameter value status with the string error, you can determine that the loading action resulted in an error.

With this, as it seems all your error Urls change only as to HTTP Response Code, simply concatenate the immutable part of the string to the status code:

$('#conteudo').load($(this).attr('href'), function(responseText, statusText, xhr)
                {
    if( status == "error" ) {

        $( '#conteudo' ).load( '/error/error' + xhr.status );
    }
});

Just note that you must have a fallback for case the second call to jQuery.load(), to error, also fail and you have how to debug.

As for the second question, its biggest problem is the Javascript obstruction. A Web Application should only work with HTML and server-side programming. Then you add CSS and Javascript.

That is, your programming lad server, when receiving a certain URL will have to return as a response to the browser everything that the GUI needs to work, this includes menu and body.

After the visual interface is ready is that the JS would make it richer, allowing dynamic navigation, for example.

  • Why, truth had not even thought of this detail, it was worth taking advantage of the package, you would recommend another form of control or is sufficiently "good"?

  • I’m going to need a little more context. What exactly do you mean?

  • added in the question Edit, if you can take a look, thank you, thanks

  • I understood I think, I would put a header in the index, if the server received header means q came from the clicked links then do as scheduled, otherwise the user typed and gave enter, then sends something q knife load in the /menu, right?

  • Maybe that can help you make a control.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.