How to change error message in lightbox upon Ajax submission error?

How to change error message in lightbox upon Ajax submission error?

daveslabdaveslab Posts: 40Questions: 14Answers: 0

Hi folks,

Simple question: for Datatables Editor used with the lightbox, if there's an HTTP error code in the response, Datatables will simply show an error message saying to contact the system administrator. If, however, I would like to show a custom error message based on the HTTP error code, it doesn't seem possible. Am I missing something? Please note that I could change the message text via the internationalization options but I want to be able to post multiple custom error messages and not just one global one. Here's a snippet of my code:

///...
if (jqxhr.responseText.includes("Unable to login with provided credentials.")) {
  editor.error('Identifiants incorrects. Veuillez réessayer.');
}

errorCallback(jqxhr, textStatus, errorThrown);
//...

This question has an accepted answers - jump to answer

Answers

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

    Listen for the submitError event and call error() inside that. The xhr object is passed in as the second parameter to submitError so you can inspect that for the status code.

    Allan

  • daveslabdaveslab Posts: 40Questions: 14Answers: 0

    Great, thanks! Here's what I ended up doing:

    //...
    editor.on('submitError', function(e, xhr, err, thrown, data) {
      if (xhr.status == 400 && xhr.responseText.includes("Unable to login with provided credentials.")) {
        editor.error('Identifiants incorrects. Veuillez réessayer.');
      }
    });
    //...
    
This discussion has been closed.