javascript - XMLHttpRequest.responseText is not rendering the json encoded output
Get the solution ↓↓↓My$request->generate( 'web_notifier' )
generated link:
header( 'Content-Type: application/json' );
# Die is used to ensure nothing else is being outputted further down the script
die( json_encode ( array(
'session' => array(
'token' => $request->session( 'crsf_token' )
)
) ) );
When I visit this API link, I get this output (and I can see that the Content-Type in the header is set to application/json:
{"session":{"token":"3d096c30a00da9ab37019122622dac80edec6db0cf006d5f4450ecd8d813c692"}}
However, if I now try to use aXMLHttpRequest
to view thesession.token
, like below, I getCannot read property 'token' of undefined
which tells me that it has not been rendered as JSON.
let req = new XMLHttpRequest();
req.onload = () => {
console.log(req.responseText.session.token);
console.log(req.responseText['session']['token']);
};
req.open( 'GET', '{{$request->generate( 'web_notifier' )}}' );
req.send();
I did aconsole.log(req.responseText)
and can see that it is in String format - Why is this if I have specified the Content-Type correctly and usedjson_encode()
? How can I fix this?
Answer
Solution:
The json parsing is not automatic you need to passe your response into JSON.parse(str), also i advise you to use fetch instead of XMLHttpRequest it has a builin conversion.
fetch(url).then(res => res.json()).then(handler)
jsonString = '{"data": "some_data"}'
console.log(JSON.parse(jsonString))
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the metadata storage is not up to date, please run the sync-metadata-storage command to fix this issue.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
JavaScript
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
Welcome to programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.