Premium support for our pure JavaScript UI components


Post by lakshmi p »

Hi Team,
I'm using an AjaxStore. One of my rest calls fails and there is an error msg in the response payload.
But I'm unable to access it in the code.
In afterRequest listener ,the props param is as shown in screenshot.
Any pointers to how can i access response payload?
I need to get the message shown in network tab but the props parameter doesnot contain it

Attachments
response.png
response.png (88.97 KiB) Viewed 151 times
props.png
props.png (70.16 KiB) Viewed 151 times

Post by khattakdev »

How does your response looks like in the afterRequest?
It should be there at response.error when you get a response back.

store.on(
  "afterRequest",
  ({ exception, action, exceptionType, response, json }) => {
    console.log(response.error);
  }
);

Arsalan
Developer Advocate


Post by lakshmi p »

This is what response.error contains:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

and not the message present in the response body.

Attaching a screenshot of response

Attachments
responseAfterReq.png
responseAfterReq.png (78.44 KiB) Viewed 145 times

Post by khattakdev »

It looks like you are sending a plain text from backend.

Try sending in JSON format, for example:

return res.status(400).json({
    error: "Something went wrong!!",
  });

which can be then seen on frontend

store.on(
  "afterRequest",
  ({ exception, action, exceptionType, response, json }) => {
    console.log(response.parsedJson.error);
  }
);

Arsalan
Developer Advocate


Post by lakshmi p »

is it possible to obtain this plainText itself?


Post by mats »

Is this what you're looking for?

console.log(response.parsedJson.error.message);

Post by lakshmi p »

response.parsedJson is null in my case.
When a REST call fails, the error message is available in the response body as a plainText
How to access it in the afterRequest listener or in the store commit's failure fn?


Post by alex.l »

Hi,

Try to put debugger into "afterRequest" handler and learn what's in response object. Find where is your text placed and use it.
I believe that's easy to get. It might be in response as is.

Please post vanilla related questions in vanilla forums, not in ExtJS forums. It will allow us to reply faster.
Thank you!

All the best,
Alex Lazarev

How to ask for help? Please read our Support Policy

We do not write the code in bounds of forum support. If you need help with development, contact us via bryntum.com/services


Post by lakshmi p »

HI Team, i couldn't find the message in the response object.


Post by khattakdev »

Hi,

I can't say much about the issue. Could you please share a runnable test so we can debug it? I am specifically interested in the piece of code where you're accessing the response object but having a runnable test help us to debug it better.

Arsalan
Developer Advocate


Post Reply