第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用帶有 React 的 Axios 發(fā)布到快遞后端時(shí)遇到問題

使用帶有 React 的 Axios 發(fā)布到快遞后端時(shí)遇到問題

qq_笑_17 2022-07-21 10:58:26
我正在使用 React 和 Nextjs,并在端口 3001 上運(yùn)行 Express 后端。我正在嘗試讓我的注冊頁面將信息發(fā)布到數(shù)據(jù)庫,但我收到了一組錯(cuò)誤編輯 - 修復(fù) CORS 問題,發(fā)布到數(shù)據(jù)庫時(shí)仍有問題編輯 - 這是新的錯(cuò)誤:POST http://localhost:3001/users/signup 404 (Not Found)這個(gè)未捕獲的承諾錯(cuò)誤:createError.js:16 Uncaught (in promise) Error: Network Error    at createError (createError.js:16)    at XMLHttpRequest.handleError (xhr.js:83)我認(rèn)為問題出在 axios 帖子上,但我不確定。因?yàn)槲乙呀?jīng)啟用了 Cors,并且在 axios 選項(xiàng)的標(biāo)題中也啟用了,所以我不確定問題是什么..后端:應(yīng)用程序.jsconst express = require("express");const app = express();const cors = require("cors");const bodyParser = require("body-parser");const logger = require("morgan");const session = require("express-session");const FileStore = require("session-file-store")(session);const upload = require("express-fileupload");app.use(upload());console.log("Server Started!");app.use(function (req, res, next) {  res.header("Access-Control-Allow-Origin", "*");  res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");  res.header(    "Access-Control-Allow-Headers",    "Origin, X-Requested-With, Content-Type, Accept"  );  next();});app.use(logger("dev"));app.use(cors);app.use(bodyParser.urlencoded({ extended: true }));app.use(bodyParser.json());app.use(  session({    resave: false,    secret: "hello",    saveUninitialized: true,    is_logged_in: false,  }));const indexRouter = require("./routes/index");app.use("/", indexRouter);app.get('/', function (req, res) {  res.send('<h1>hello world</h1>')});module.exports = app;我不確定后端還需要什么,所有路由和東西都由 nextjs 處理。
查看完整描述

1 回答

?
繁星點(diǎn)點(diǎn)滴滴

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超3個(gè)贊

cors是一個(gè)工廠函數(shù),當(dāng)調(diào)用它時(shí)返回 Express 中間件函數(shù),該函數(shù)設(shè)置允許傳入的跨源請求所需的 CORS 標(biāo)頭。

app.use(cors()); // instead of app.use(cors)

鏈接到文檔

此外,axios您必須在端點(diǎn) URL 前加上協(xié)議部分。

.post("http://localhost:3001/users/signup") // you were missing "http://"


查看完整回答
反對 回復(fù) 2022-07-21
  • 1 回答
  • 0 關(guān)注
  • 364 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)