javascript - 方法发布不适用于 Axios 和 React JS

标签 javascript reactjs react-native axios

我是 React 的新手。我正在寻找如何在数据库中保存带有 axios 的表单数据。但是不工作..

我的 API.JS

const axios = require ("axios");
const querystring = require('querystring');

export function getPeople(){
  return axios.get(`http://127.0.0.1:9000/people`)
}

export function postPeople(){
  axios.post('http://127.0.0.1:9000/people', querystring.stringify({
    'bar': 123
  }));
}

我的 app.js:

import React, { Component } from 'react';
import { getPeople, postPeople } from './api';

addItem = () => {
    postPeople();
}

我的 Express.js:

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


app.get('/people', cors(), function (req, res, next) {
  res.json([
    {
      id: 0,
      name: "0",
      age: 20,
      city: 'R0eiro',
      country: '04'
    },
    {
      id: 1,
      name: "0",
      age: 29,
      city: 'Minas 00',
      country: '00'
    },
})

app.listen(9000, function () {
  console.log('The server is running in the port 9000')
})

给出错误:

POST http://127.0.0.1:9000/people 404 (Not Found)

Failed to load http://127.0.0.1:9000/people: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 404.

Uncaught (in promise) Error: Network Error at createError (createError.js:16) at XMLHttpRequest.handleError (xhr.js:87)

有人帮帮我吗?

最佳答案

您遇到了 CORS 问题。

取自this SO answer :

Access-Control-Allow-Origin is a CORS (Cross-Origin Resource Sharing) header.

When Site A tries to fetch content from Site B, Site B can send an Access-Control-Allow-Origin response header to tell the browser that the content of this page is accessible to certain origins. (An origin is a domain, plus a scheme and port number.) By default, Site B's pages are not accessible to any other origin; using the Access-Control-Allow-Origin header opens a door for cross-origin access by specific requesting origins.

For each resource/page that Site B wants to make accessible to Site A, Site B should serve its pages with the response header:

Access-Control-Allow-Origin: http://siteA.com Modern browsers will not block cross-domain requests outright. If Site A requests a page from Site B, the browser will actually fetch the requested page on the network level and check if the response headers list Site A as a permitted requester domain. If Site B has not indicated that Site A is allowed to access this page, the browser will trigger the XMLHttpRequest's error event and deny the response data to the requesting JavaScript code.

您可能需要 cors npm package :

从像安装cors这样的命令:

npm install cors

然后在你的 API.js 中:

const axios = require ("axios");
const querystring = require('querystring');
const cors = require('cors')
app.use(cors())

export function getPeople(){
  return axios.get(`http://127.0.0.1:9000/people`)
}

export function postPeople(){
  axios.post('http://127.0.0.1:9000/people', querystring.stringify({
    'bar': 123
  }));
}

关于javascript - 方法发布不适用于 Axios 和 React JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49951238/

相关文章:

javascript - 动态加载 100 多个节点时 Dynatree 速度较慢

Javascript:通过过滤对象列表过滤对象列表

reactjs - 无法解析'@material-ui/core/TableContainer

css - 仅在需要时加载 React block CSS

javascript - PHP - 是否可以在一个文件中获取动态生成的值,以便在 if 语句中在另一个文件中使用?

javascript - 当用户向下滚动窗口 JAVASCRIPT 时,将 div 的颜色淡化为不透明

react-native - Expo.js : Capture event when my app goes into the background?

javascript - 在 React Native 中连接日期和时间返回 'Invalid Date'

javascript - 为什么 Visual Studio Code 会自动添加 {""}?

javascript - Vue.js 中的 Messenger 扩展并将其数据用作全局变量