如何将React Native应用与后端Django连接

huangapple go评论95阅读模式
英文:

How to connect react native app with backend django

问题

以下是你要翻译的内容:

"So I have a backend with django and I am using react native for frontend. I added an emulator in android studio.

And when I start the backend like: python manage.py runserver I can run also the frontend with expo start.

But now I want to fetch the data with react native. So I start the backend with: python manage.py runserver 192.168.1.68:19000 - settings file of django:

  1. INSTALLED_APPS = [
  2. 'corsheaders',
  3. 'DierenWelzijnAdmin.apps.DierenwelzijnadminConfig',
  4. 'django.contrib.admin',
  5. 'django.contrib.auth',
  6. 'django.contrib.contenttypes',
  7. 'django.contrib.sessions',
  8. 'django.contrib.messages',
  9. 'django.contrib.staticfiles',
  10. 'rest_framework',
  11. 'rest_framework.authtoken',
  12. ]
  13. MIDDLEWARE = [
  14. 'django.middleware.security.SecurityMiddleware',
  15. 'django.contrib.sessions.middleware.SessionMiddleware',
  16. 'corsheaders.middleware.CorsMiddleware',
  17. 'django.middleware.common.CommonMiddleware',
  18. 'django.middleware.csrf.CsrfViewMiddleware',
  19. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  20. 'django.contrib.messages.middleware.MessageMiddleware',
  21. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  22. ]
  23. ALLOWED_HOSTS = ['192.168.1.68', '127.0.0.1', ]
  24. CORS_ORIGIN_ALLOW_ALL = False
  25. CORS_ORIGIN_WHITELIST = [
  26. "http://localhost:8081",
  27. "http://localhost:19006",
  28. "http://localhost:19002",
  29. "http://192.168.1.69:8000",
  30. "http://192.168.1.68:19000",
  31. ]
  32. CORS_ALLOW_METHODS = [
  33. "DELETE",
  34. "GET",
  35. "OPTIONS",
  36. "PATCH",
  37. "POST",
  38. "PUT",
  39. ]
  40. CORS_ALLOW_HEADERS = [
  41. "accept",
  42. "accept-encoding",
  43. "authorization",
  44. "content-type",
  45. "dnt",
  46. "origin",
  47. "user-agent",
  48. "x-csrftoken",
  49. "x-requested-with",
  50. ]

And react native service:

  1. export const fetchCategoryData = async () => {
  2. try {
  3. const response = await fetch("http://192.168.1.68:19000/animal/categories/main_groups/", {
  4. method: "GET",
  5. headers: {
  6. Accept: "application/json",
  7. "Content-Type": "application/json",
  8. },
  9. });
  10. if (!response.ok) {
  11. throw an Error("Network response was not ok");
  12. }
  13. return await response.json();
  14. //setCategoryData(data);
  15. } catch (error) {
  16. console.error("There was a problem with the fetch operation:", error);
  17. throw error;
  18. }
  19. };

And when I then start the react native app with expo start and I do a r after a.

I get this message:

  1. warn No apps connected. Sending "reload" to all React Native apps failed. Make sure your app is running in the simulator or on a phone connected via USB.

The emulator I installed from android studio is: Pixel 2 XL and if I do: adb devices I see the emulator:

  1. List of devices attached
  2. emulator-5554 device

And I already whiped the data several times from the emulator.

Question:

How to run the react native for fetching data from the backend?"

英文:

So I have a backend with django and I am using react native for frontend. I added a emulator in android studio.

And when I start the backend like: python manage.py runserver I can run also the frontend with expo start.

But now I want to fetch the data with react native. So I start the backend with:
python manage.py runserver 192.168.1.68:19000 - settings file of django:

  1. INSTALLED_APPS = [
  2. 'corsheaders',
  3. 'DierenWelzijnAdmin.apps.DierenwelzijnadminConfig',
  4. 'django.contrib.admin',
  5. 'django.contrib.auth',
  6. 'django.contrib.contenttypes',
  7. 'django.contrib.sessions',
  8. 'django.contrib.messages',
  9. 'django.contrib.staticfiles',
  10. 'rest_framework',
  11. 'rest_framework.authtoken',
  12. ]
  13. MIDDLEWARE = [
  14. 'django.middleware.security.SecurityMiddleware',
  15. 'django.contrib.sessions.middleware.SessionMiddleware',
  16. 'corsheaders.middleware.CorsMiddleware',
  17. 'django.middleware.common.CommonMiddleware',
  18. 'django.middleware.csrf.CsrfViewMiddleware',
  19. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  20. 'django.contrib.messages.middleware.MessageMiddleware',
  21. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  22. ]
  23. ALLOWED_HOSTS = ['192.168.1.68', '127.0.0.1', ]
  24. CORS_ORIGIN_ALLOW_ALL = False
  25. CORS_ORIGIN_WHITELIST = [
  26. "http://localhost:8081",
  27. "http://localhost:19006",
  28. "http://localhost:19002",
  29. "http://192.168.1.69:8000",
  30. "http://192.168.1.68:19000",
  31. ]
  32. CORS_ALLOW_METHODS = [
  33. "DELETE",
  34. "GET",
  35. "OPTIONS",
  36. "PATCH",
  37. "POST",
  38. "PUT",
  39. ]
  40. CORS_ALLOW_HEADERS = [
  41. "accept",
  42. "accept-encoding",
  43. "authorization",
  44. "content-type",
  45. "dnt",
  46. "origin",
  47. "user-agent",
  48. "x-csrftoken",
  49. "x-requested-with",
  50. ]

And react native service:

  1. export const fetchCategoryData = async () => {
  2. try {
  3. const response = await fetch("http://192.168.1.68:19000/animal/categories/main_groups/", {
  4. method: "GET",
  5. headers: {
  6. Accept: "application/json",
  7. "Content-Type": "application/json",
  8. },
  9. });
  10. if (!response.ok) {
  11. throw new Error("Network response was not ok");
  12. }
  13. return await response.json();
  14. //setCategoryData(data);
  15. } catch (error) {
  16. console.error("There was a problem with the fetch operation:", error);
  17. throw error;
  18. }
  19. };

And when I then start the react native app with expo start and I do a r after a.

I get this message:

  1. warn No apps connected. Sending "reload" to all React Native apps failed. Make sure your app is running in the simulator or on a phone connected via USB.

The emulator I installed from android studio is: Pixel 2 XL and if I do: adb devices
I see the emulator:

  1. List of devices attached
  2. emulator-5554 device

And I already whiped the data several times from the emulator.

Question:

How to run the react native for fetching data from the backend?

答案1

得分: 0

我已经更改了后端为:

192.168.1.68:8000

而且前端也是:

192.168.1.68:8000

现在可以正常工作了 如何将React Native应用与后端Django连接

这确实是我非常愚蠢的错误。因为我曾经获取了'//192.168.1.68:19000'。但那是我的模拟器主机。当然不会起作用。

英文:

Oke, I changed the backend to:

> 192.168.1.68:8000

and in frontend also:

> 192.168.1.68:8000

and it works now 如何将React Native应用与后端Django连接

This was indeed very stupid of me. Because I did fetch('//192.168.1.68:19000'). But that is my emulator host. Of course that doesn't work

huangapple
  • 本文由 发表于 2023年2月13日 23:38:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定