Error with JSON.parse in React-Native + Xios

Asked

Viewed 179 times

0

I’m using Aces to fetch data from a PHP API. I get a reply with the data expected in React-Native, however when using the function JSON.parse() get the bug:

"[Syntaxerror: JSON Parse error: Unrecognized token '']".

I have already seen the answer I receive using the tool available on https://jsonlint.com/.

The full answer I get is:

    {"config": {"adapter": [Function xhrAdapter], "baseURL": "baseurl.com.br", "data": "{\"data\":{\"codPedido\":\"67912\",\"codLoja\":\"3\",\"action\":\"getPedido\"}}", "headers": {"Accept": "application/json, text/plain, */*", "Content-Type": "application/json;charset=utf-8"}, "maxBodyLength": -1, "maxContentLength": -1, "method": "post", "timeout": 0, "transformRequest": [[Function transformRequest]], "transformResponse": [[Function transformResponse]], "url": "/request.php", "validateStatus": [Function validateStatus], "xsrfCookieName": "XSRF-TOKEN", "xsrfHeaderName": "X-XSRF-TOKEN"}, "data": "{\"status\":\"success\",\"type\":true,\"data\":{\"codLoja\":\"3\",\"codPedido\":\"67912\"}}", "headers": {"connection": "Keep-Alive", "content-type": "text/html; charset=UTF-8", "date": "Mon, 23 Nov 2020 18:27:54 GMT", "keep-alive": "timeout=5, max=100", "server": "Apache", "vary": "Accept-Encoding,User-Agent"}, "request": {"DONE": 4, "HEADERS_RECEIVED": 2, "LOADING": 3, "OPENED": 1, "UNSENT": 0, "_aborted": false, "_cachedResponse": undefined, "_hasError": false, "_headers": {"accept": "application/json, text/plain, */*", "content-type": "application/json;charset=utf-8"}, "_incrementalEvents": false, "_lowerCaseResponseHeaders": {"connection": "Keep-Alive", "content-type": "text/html; charset=UTF-8", "date": "Mon, 23 Nov 2020 18:27:54 GMT", "keep-alive": "timeout=5, max=100", "server": "Apache", "vary": "Accept-Encoding,User-Agent"}, "_method": "POST", "_perfKey": "network_XMLHttpRequest_https://baseurl.com.br/request.php", "_requestId": null, "_response": "{\"status\":\"success\",\"type\":true,\"data\":{\"codLoja\":\"3\",\"codPedido\":\"67912\"}}", "_responseType": "", "_sent": true, "_subscriptions": [], "_timedOut": false, "_trackingName": "unknown", "_url": "baseurl.com.br/request.php", "readyState": 4, "responseHeaders": {"Connection": "Keep-Alive", "Content-Type": "text/html; charset=UTF-8", "Date": "Mon, 23 Nov 2020 18:27:54 GMT", "Keep-Alive": "timeout=5, max=100", "Server": "Apache", "Vary": "Accept-Encoding,User-Agent"}, "responseURL": "baseurl.com.br/request.php", "status": 200, "timeout": 0, "upload": {}, "withCredentials": true}, "status": 200, "statusText": undefined}

I try to use JSON.parse in the date property:

    api.post('/request.php', {
                data: { codPedido, codLoja, action: 'getPedido' },  
            })
            .then(data => {
                console.log(data);
                console.log(JSON.parse(data.data));
            })
            .catch(error => {
                console.log(error);
            })

Has anyone gone through this or knows what I might be doing wrong?

Versions used

  • React-Native: 0.63.3
  • Xios: 0.21.0
  • API php: 5.6.40
  • Testing on: LG K50S (android 9)
  • 1

    I believe that this mistake is due to the fact that JSON.parser needless. Axios already treats the answers to Voce serving you a javascript object that you can already use, so you don’t need to parse manually, so Voce is parsing a javascript object and not a json.

  • Thanks for the answer. I checked the type of data.data using typeof data.data and the answer is 'string'. So I can’t access properties like type and status

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.