\"JSON Parse error: Unrecognized token\'<\'\" Error is showing while hitting the api. Code is attached below Note* : Response is in the JSON format.
I found this issue the upload the images on amazon S3 and image size is large. I upload the image on lower resolution and it's working fine for me.
check your API; sometimes you need to set error_reporting(0);
if you're working with PHP
This Means you are getting Html response from the server probably a 404 or 500 error. Instead of response.json()
use response.text()
you will get the html in text.
fetch("http:/example.com", {method: "POST",
body: JSON.stringify(
{
uname: uname,
password: password
}
)
})
.then((response) => response.text())
.then((responseData) => {
AlertIOS.alert(
"POST Response",
"Response Body -> " + responseData
)
}).done();
this.props.navigation.navigate("Home")
};
In my case, I was using Infinity Free WebHosting for my API design and tests and apparently they dont allow exposure of API endpoints if you are using their free account.
That error means you are getting HTML
code sent back to you and you have to do a console.log(data_response)
to see the actual error. It is much easier if you are using Axios
Error that i was getting:
<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("c353693e65094689705f1b8cec0deb51");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>
Solution:
Try another different Shared Hosting service provider.
You can try by adding the headers to your fetch api, as it posts your record to your url.
var dataObj = {}
dataObj.uname = uname,
dataObj.password = password
fetch("http:/example.com", {
method: 'post',
headers: {
'Accept': 'application/json, text/plain, */*', // It can be used to overcome cors errors
'Content-Type': 'application/json'
},
body: JSON.stringify(dataObj)
})
.then((response) => response.json())
.then((responseData) => {
AlertIOS.alert(
"POST Response",
"Response Body -> " + JSON.stringify(responseData.body)
)
}).done();
this.props.navigation.navigate("Home")
};
this is likely when you receive html tag format from the server not the json check out the server encoding the data into proper json format you can check response by response.text() if it works than you are receiving text format.