Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Axios - How to read JSON response?

Axios 0.17.1

.then(function (response) {                 console.log(response);                 //console.log(response.status);                 //It is an error -> SyntaxError: Unexpected token u in JSON at position 0                  console.log(JSON.parse(response.data.error));                 console.log(response.data.error); //undefined. 

The console.log of response is

{data: "{"error":"Name must be entered with more than one … NULL↵
["isPipe":protected]=>↵ NULL↵ }↵}↵", status: 203, statusText: "Non-Authoritative Information", headers: {…}, config: {…}, …} config : {adapter: ƒ, transformRequest: {…}, transformResponse: {…}, timeout: 0, xsrfCookieName: "XSRF-TOKEN", …} data : "{"error":"Name must be entered with more than one character."}object(Slim\Http\Response)#32 (5) {↵ ["status":protected]=>↵ int(200)↵ ["reasonPhrase":protected]=>↵ string(0) ""↵ ["protocolVersion":protected]=>↵ string(3) "1.1"↵ ["headers":protected]=>↵ object(Slim\Http\Headers)#33 (1) {↵
["data":protected]=>↵ array(1) {↵ ["content-type"]=>↵
array(2) {↵ ["value"]=>↵ array(1) {↵ [0]=>↵
string(24) "text/html; charset=UTF-8"↵ }↵
["originalKey"]=>↵ string(12) "Content-Type"↵ }↵ }↵ }↵ ["body":protected]=>↵ object(Slim\Http\Body)#31 (7) {↵
["stream":protected]=>↵ resource(59) of type (stream)↵
["meta":protected]=>↵ NULL↵ ["readable":protected]=>↵ NULL↵
["writable":protected]=>↵ NULL↵ ["seekable":protected]=>↵
NULL↵ ["size":protected]=>↵ NULL↵ ["isPipe":protected]=>↵
NULL↵ }↵}↵" headers : {content-type: "application/json;charset=utf-8"} request : XMLHttpRequest {onreadystatechange: ƒ, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …} status : 203 statusText : "Non-Authoritative Information" proto : Object

JSON.parse(response.data) as well as response.data.error -> Both are giving error. How can i read the data?

Slimframework 3.

$data = array('error' => 'Name must be entered with more than one character.');         $newResponse = $response->withJson($data, 203);         return $newResponse; 
like image 315
Mahesh Avatar asked Jan 02 '18 14:01

Mahesh


People also ask

Does Axios automatically parse JSON?

When making a POST or PUT request, Axios will automatically parse the data to JSON, provided you are sending an object, and make the necessary adjustments elsewhere in the request so it can be automatically parsed once received by the server.

How do I get an Axios post response?

To perform an HTTP POST request in Axios, call axios. post() . Making a POST request in Axios requires two parameters: the URI of the service endpoint and an object that contains the properties you wish to send to the server. For a simple Axios POST request, the object must have a url property.

How do you change Axios response to string?

If the response is a JSON object, you can just use JSON. stringify() to get the response data as a string.

Is Axios a JSON?

By default, Axios converts Javascript data to JSON (including AJAX). The “content-type” header is also set to “application/json.” If you send a serialized JSON object as data, however, Axios considers it as “application/x-www-form-urlencoded” (form-encoded request body).


2 Answers

In Axios responses are already served as javascript object, no need to parse, simply get response and access data.

like image 136
Mosè Raguzzini Avatar answered Sep 30 '22 10:09

Mosè Raguzzini


Assuming the response from the server looks like this:

{"token": "1234567890"} 

Then in Axios you can access it like this:

console.log( response.data.token ) 
like image 28
Robert Reiz Avatar answered Sep 30 '22 10:09

Robert Reiz