【问题标题】:how to fix network error on Apisauce when coonecting with backend与后端连接时如何修复 Apisauce 上的网络错误
【发布时间】:2021-03-24 15:43:39
【问题描述】:

我正在使用 API 酱连接到我的后端,我已经仔细检查了我的所有参数是否即将到来,并且也使用 Postman 进行了测试,并且一切正常。无论出于何种原因,它只是没有连接到后端,当我 console.log 得到结果时。它的节目

Object {
  "config": Object {
    "adapter": [Function xhrAdapter],
    "baseURL": "http://127.0.0.1:8000/api",
    "data": "{\"username\":\"adeyeanqar \",\"password\":\"Liverpool8\"}",
    "headers": Object {
      "Accept": "application/json",
      "Content-Type": "application/json",
    },
    "maxBodyLength": -1,
    "maxContentLength": -1,
    "method": "post",
    "timeout": 0,
    "transformRequest": Array [
      [Function transformRequest],
    ],
    "transformResponse": Array [
      [Function transformResponse],
    ],
    "url": "/token/",
    "validateStatus": [Function validateStatus],
    "xsrfCookieName": "XSRF-TOKEN",
    "xsrfHeaderName": "X-XSRF-TOKEN",
  },
  "data": null,
  "duration": 12,
  "headers": null,
  "ok": false,
  "originalError": [Error: Network Error],
  "problem": "NETWORK_ERROR",
  "status": null,
}

这是我的客户

import { create } from "apisauce";

const apiClient = "http://127.0.0.1:8000/api",
});

这是身份验证的端点

import client from "./client";

const login = (username, password) =>
  client.post("/token/", { username, password });

export default {
  login,
};

通常它应该返回一个令牌,当我控制台结果时。

【问题讨论】:

  • 您应该使用 cors 来启用

标签: javascript reactjs react-native axios apisauce


【解决方案1】:

您应该使用 cors 以便通过各种选项启用 CORS。 为此使用以下库: npm install cors

    var express = require('express')
var cors = require('cors')
var app = express()

app.use(cors())

app.get('/products/:id', function (req, res, next) {
  res.json({msg: 'This is CORS-enabled for all origins!'})
})

app.listen(80, function () {
  console.log('CORS-enabled web server listening on port 80')
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 2021-03-20
    • 2022-01-13
    • 1970-01-01
    相关资源
    最近更新 更多