【问题标题】:"You need to enable JavaScript to run this app"; response is html instead of json“您需要启用 JavaScript 才能运行此应用程序”;响应是 html 而不是 json
【发布时间】:2022-01-25 03:45:18
【问题描述】:

使用的技术:Django、Djoser、react.js、webpack

我在 react.js 中提出了请求:

const config = {
    headers: {
      "Content-Type": "application/json",
    },
  };

  const body = JSON.stringify({
    email,
    first_name,
    last_name,
    password,
    re_password,
  });
const response = await axios.post(
      `http://127.0.0.1:8000/auth/users/`,
      body,
      config
    )

我在后端使用 Djoser 来处理身份验证请求。

我 console.log 响应并发现响应是 html 而不是 JSON。状态是 200,但根据 Djoser 的文档,它应该是 HTTP_201_CREATED。

我检查了 Djoser 在 Django 中的 INSTALLED_APPS 中,我有 djoser==2.1.0。 我尝试在 package.json 中添加"proxy": "http://127.0.0.1:8000",,但不起作用。我也尝试使用 fetch 而不是 axios,也不起作用。我不确定问题出在哪里。

【问题讨论】:

    标签: reactjs django webpack djoser


    【解决方案1】:

    我想通了。

    状态码是有线的,看起来它没有调用 Djoser 端点。

    我发现在我的config.urls(后端)中有

    urlpatterns = [
        path('', include('frontend.urls')),
        path('auth/', include('djoser.urls')),
        path('auth/', include('djoser.urls.jwt'))
    ]
    

    所以auth/ 端点将通过 frontend.urls 而不是 Djoser。

    我改成之后

    urlpatterns = [
        path('auth/', include('djoser.urls')),
        path('auth/', include('djoser.urls.jwt')),
        path('', include('frontend.urls')),
    ]
    

    它有效。

    【讨论】:

      猜你喜欢
      • 2021-02-06
      • 2019-12-16
      • 2020-05-09
      • 2022-11-30
      • 2021-10-03
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      相关资源
      最近更新 更多