简体   繁体   中英

Django REST API connection issues

I am new-ish to using Django, so this may be an easy thing for someone...I hope so!

I have built a Django REST API by following the tutorial found here: http://www.django-rest-framework.org/ . When I run the server, I am able to use the Browsable API and navigate through the API, such as specifically this link: http://127.0.0.1:8000/snippets/ . I am also able to access this URL through Postman GET request and retrieve the associated Snippets.

Now, here is my problem:

I have created a React Native application where I am fetching data from this URL (my python server is running). I console.log the response from this, and I continually get an error of:

Error: Network Error
    at createError (createError.js:15)
    at XMLHttpRequest.handleError (xhr.js:87)
    at XMLHttpRequest.dispatchEvent (event-target.js:172)
    at XMLHttpRequest.setReadyState (XMLHttpRequest.js:478)
    at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:324)
    at XMLHttpRequest.js:418
    at RCTDeviceEventEmitter.emit (EventEmitter.js:179)
    at MessageQueue.__callFunction (MessageQueue.js:236)
    at MessageQueue.js:108
    at guard (MessageQueue.js:46)

I can refresh the link in the browser or Postman and have no issues, but in the fetch...I am unable to have any success, and am only getting back the vague error message above.

I thought it might have to be with a CSRF, or CORS...but I have both of those set-up ok in Django (and the error message doesn't say those so I don't think that is it).

Any suggestions/ideas?

Thanks!

Not that anyone could have specifically known this from my posting...but I would like to post this answer regardless for someone who may have had this same issue.

I have been developing the React Native application via Expo, and deploying it to my physical phone rather than the iOS/Android simulators.

This being the case, clearly my http://127.0.0.1:8000 or http://localhost:etc would not work from my phone! I then switched to using the iOS Simulator and it worked the first try without problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM