redirect to href when ajax error

redirect to href when ajax error

LapointeLapointe Posts: 430Questions: 81Answers: 4

Hi all
Is there a way to set default return url when ajax error ?
When user disconnect I close current session.

If user go to previous page (history back) the page is displayed and a datatable message explain data was not set correctly.

So is there a way to set no message and url to redirect (in my case login page) ?

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,146Questions: 1Answers: 2,586

    I don't quite follow, but you can set the URL with ajax.url(). You could also test the ajax before initialising the table, or possibly ensuring valid JSON is sent.

    Colin

  • LapointeLapointe Posts: 430Questions: 81Answers: 4

    Hi @colin
    Thanks for answer

    In fact url is set correctly to get data.
    When user click on disconnect I unset cookie session.

    At each page start, I ensure session is ok using php code, and if not I redirect to login page using header(...), but going to previous page do not execute php, just display computed html page

    So when a history back is clicked on browser, the previous page is displayed, ajax is called and data can not be found, because no cookie is set to ensure who is online.
    The message about invalid json is displayed.

    If I click on refresh page, php is launched again and I am redirected to the login screen.

    You tell me I can ensure valid JSON is send.... I try an do at ajax url page start verify if session condition are OK. If not I just return json_encode(""), so no message is displayed, but page stay displayed explaining waiting for data.

    So if an error return url should be set on malformed json response, the redirect page should be displayed with no message.

    Hope my explanation are clear...

  • allanallan Posts: 61,740Questions: 1Answers: 10,111 Site admin
    Answer ✓

    There are two options here:

    1. Return valid JSON (since that is what DataTables expects), and include in it a parameter that indicates that the session has timed out. You can use a listener on xhr to see that parameter and subsequently call window.location.reload().
    2. Use a custom error handler which will do that.

    I'd say option 1 would be easiest.

    Allan

  • LapointeLapointe Posts: 430Questions: 81Answers: 4

    Hi @allan

    Thanks.
    I'll have a look in this direction.

    Bob

This discussion has been closed.