Request state
Liquid Ajax Cart keeps the information about each Shopify Cart Ajax API request in Javascript objects that are called “Request state”.
The objects usually passed as a parameter to the callbacks that you define using the Cart Ajax API requests functions.
The object looks like this:
{
"endpoint": "/cart/add.js",
"requestBody": {"items": [{"id": 40934235668668, "quantity": 1}], "sections": "my-cart"},
"requestType": "add",
"info": {
"initiator": Element {}
},
"responseData": {
"ok": true,
"status": 200,
"body": {…}
},
"extraResponseData": {
"ok": true,
"status": 200,
"body": {…}
}
}
requestBodyis JSON orFormDataobject forPOSTrequests andundefinedforGETrequests.info— additional data that set by a request caller.- Product forms and Controls attach the
initiatorproperty to theinfoparameter. Theinitiatorkeeps theHTMLElementobject of the form or control. - If there is a
cancelproperty of theinfoparameter andcancelproperty istrue, then the request will not be performed and theresponseDatawill not exist.
- Product forms and Controls attach the
responseData— the response on the request.okistrueif the response was successful (status in the range 200-299).- for
/cart/add.jsrequests Liquid Ajax Cart performs an extraPOSTrequest to the/cart/update.jsendpoint to get the updated cart state.extraResponseDataobject contains the response of the extra request. - Sometimes Liquid Ajax Cart performs an additional Shopify
POST /cart/update.jsrequest and saves the response in theextraResponseDataobject. The additional request happens if:- there were a
/cart/add.jsrequest that doesn’t return the cart JSON-data — the additional/cart/update.jsrequest will bring the updated cart state, - there were a request with more than 5 sections — the additional
/cart/update.jsrequest will bring the rest sections’ HTML.
- there were a
-
If the request couldn’t be performed and response wasn’t received because, for example, internet was disconnected — the
responseDatawill not exist butfetchErrorwill be there instead:{ "endpoint": "/cart/add.js", "requestBody": {"items": [{"id": 40934235668668, "quantity": 1}], "sections": "my-cart"}, "requestType": "add", "info": {}, "fetchError": { "message": "Failed to fetch", "stack": "TypeError: Failed to fetch\n at e.fetch (h..." } }