Network Error in axios post request react-native
Network Error in axios post request react-native
This is my method to checking Auth by axios post request
CheckAuth()
let obj =
username: 'axy@gmail.com',
password: 'zzxxz'
//oauth_credentials imported
let form_data = Object.assign(obj, oauth_credentials)
axios(
method: 'post',
url: routes.oauth_token,
headers:
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
,
data: form_data
)
.then(res =>
console.log(res)
)
.catch(err =>
console.log(err)
)
I want to send post request to get token but when i try, It show a Network error in console which is below
Error: Network Error
at createError (createError.js:16)
at XMLHttpRequest.handleError (xhr.js:87)
at XMLHttpRequest.dispatchEvent (event-target.js:172)
at XMLHttpRequest.setReadyState (XMLHttpRequest.js:546)
at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:381)
at XMLHttpRequest.js:485
at RCTDeviceEventEmitter.emit (EventEmitter.js:181)
at MessageQueue.__callFunction (MessageQueue.js:306)
at MessageQueue.js:108
at MessageQueue.__guard (MessageQueue.js:269)
server.dev/oauth/token
– Hashemi Rafsan
Sep 19 '17 at 19:01
did you include the protocol?
– Mohamed Khalil
Sep 19 '17 at 19:02
yeah i include the protocol . http
– Hashemi Rafsan
Sep 19 '17 at 19:03
is it iOS & the protocol http
– Mohamed Khalil
Sep 19 '17 at 19:04
4 Answers
4
You are generating a preflighted request because your content-type
in header is application/x-www-form-urlencoded
the docs https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
content-type
application/x-www-form-urlencoded
preflighted requests first send an HTTP request by the OPTIONS method to the resource on the other domain, in order to determine whether the actual request is safe to send. Cross-site requests are preflighted like this since they may have implications to user data.
you must ensure the server can manage the OPTIONS request for it to work. or remove it from your request
Not working same error showing
– Hashemi Rafsan
Sep 19 '17 at 19:29
what auth method do you use in server?
– Mohamed Khalil
Sep 19 '17 at 19:52
async getToken(username, password)
//request server for token
try
let credential =
username: Constant.CLIENT_ID,
password: Constant.CLIENT_SECRET
;
//QueryString is a package
let data = QueryString.stringify(
username: username,
password: password,
grant_type: 'password'
);
//for ios url should be https and for android http
const requestURL = 'url';
const response = await axios.post(requestURL, data,
auth: credential,
);
console.log(response.data);
catch (error)
if (error.response.status === 401)
else
try to change the adb port. write this command
adb reverse tcp:8880 tcp:8880; adb reverse tcp:8081 tcp:8081; adb reverse tcp:8881 tcp:8881
Try running emulator with emulator -avd your_avd_name -dns-server 8.8.8.8
I mean make sure your emulator has an internet connection.
emulator -avd your_avd_name -dns-server 8.8.8.8
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
what is the value of routes.oauth_token
– Mohamed Khalil
Sep 19 '17 at 18:59