gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:54:34 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com