When I tried to post data from react-native to PHP API, react-native show the error:
If you are creating server side app or submit a form to server for register or send some data to server you need to HTTP request. In the react native you can use Axios API for the networking request like GET, POST, PUT, DELETE.In the article, we learn about React Native – Axios POST Request Submit Form With Fetch POST Request In React Native. You can also check this link for the more.
Json Parse error: Unrecognized token '<'
I tested PHP API by postman with the header type 'application/json', it works fine, here is the react-native code, can anyone help me on this? Thanks in advance!
JustinJustin
1 Answer
We just ran into this in React Native because our server was returning an error response via HTML.
The fixes could be any of the following:
1) Prevent the error from happening in your server side code.
2) Do better error handling on your server to return JSON errors instead of HTML errors.
3) Write some client side code to detect that HTML was returned and show a more useful error message.
PaulMestPaulMest2,8911 gold badge23 silver badges28 bronze badges
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Not the answer you're looking for? Browse other questions tagged phpiosjsonrestreact-native or ask your own question.
I'm having issues when using FETCH.
I am trying to make a POST request using FETCH in react-native.
When I inspect this call using Charles it is recorded as a GET request and the username and password that should be in the body are not there.
Can anyone help with this issue?
samb90samb904581 gold badge11 silver badges31 bronze badges
9 Answers
aarkerioaarkerio8801 gold badge7 silver badges22 bronze badges
I had this issue when the POST request was to an HTTPS (rather than HTTP) server. For some reason, it would somewhere along the way be converted into a GET request.
It turns out what I was doing incorrectly was sending the request to
http://myserver.com:80
rather than to https://myserver.com:443
. Once I switched it over to the proper prefix and ports, the request would properly send as a POST. Josh BakerJosh Baker
I had the same kind of issue. You have to assign the object, not sure why.
let options = {};
options.body = formdata;
options.header = header;
options.method = 'post';
user2783957user2783957
Redirection of url converts the POST request into GET requests.(Don't know why)So make sure of adding trailing arrows if any.like :'http://www.example.co.uk/login/'
aditi aditi
I had the same issue. In my case I had an extra
/
at the end of my route. I removed it and problem solved.http://google.com/someData/
to http://google.com/someData
Hesan AminilooHesan Aminiloo
If you wanna do POST request using fetch, You can do like that
Ryosuke HujisawaRyosuke Hujisawa
I have made some changes and tested it, works fine for me, check below all the changes:
Burim SylaBurim Syla
Use FormData. Problem is with JSON.stringify. You can directly import, its not third party
Rishijay ShrivastavaRishijay Shrivastava
In my case, the redirect was caused by wrongly formed url for the POST request:
http://localhost:90/Worx/drupal/d8/www//jsonapi
double slash before jsonapiBecause of the wrong url, the browser was redirecting the request and changing the method from POST to GET.
I was able to debug it after reading this:https://serverfault.com/questions/434205/nginx-https-rewrite-turns-post-to-get
Aurelian ZahaAurelian Zaha