submitError function, XHR param name, "The Ajax Object"

submitError function, XHR param name, "The Ajax Object"

rldean1rldean1 Posts: 141Questions: 66Answers: 1

In the documentation for submitError(), what is meant by the term xhr or, "The Ajax Object" (2nd parameter)? Should this be called json, instead?

I'm successfully able to invoke this event in my .fetch() try/catch block like so. In this example, my SQL server returns a "primary key" issue... It doesn't matter if I return an actual object, or if I do throw new Error

// this is a fetch try/catch block
// not shown:  data processing, attempts to return JSON
// shown: this handles problems with SQL Server

                ...

                if (message) {
                    msg = `The master stored procedure has encountered an error:\n${message}`
                };
                throw new Error (msg)
            };

        } catch (err) {

            console.error(err)
            throw new Error(err.message)
            // return err.message

        }

The submitError() event is triggered, and I actually can console.log(xhr) out the XHR object:

{
    "error": "The master stored procedure has encountered an error:\nViolation of PRIMARY KEY constraint 'PK_Retirees'. Cannot insert duplicate key in object 'dbo.Retirees'. The duplicate key value is (12345)."
}

Side note, it's the same in postSubmit, submitError, and submitComplete; however, in submitComplete, it is refered to as json.

Apologies, not trying to be pedantic, but I just want to make sure I'm using the right event, and I'm using the output correctly.

I intend to display a bootstrap alert on the screen.

Thanks,

RLD

Answers

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949

    The xhr parameter of the submitError is the actual ajax object. From the object you can interrogate the status code returned, such as 403, and the status message. This example produces an ajax error and outputs the xhr object. Depending on the error there may not be any JSON data. If there is JSON data it will be part of the xhr object.

    The json parameter of the submitSuccess and submitComplete contains only the JSON response from the server. However the postSubmit event has both the json and xhr parameters.

    Kevin

Sign In or Register to comment.