next.js 我得到的错误“跨起源请求阻止”,坚韧我有快递CORS包强硬

x759pob2  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(157)

我的应用程序代码:

import express, {Express, Request, Response, Router} from 'express';
import dotenv from 'dotenv';
import ArtistRouter from './routes/artist_routes';
import infoRouter from './routes/info_router';
import bodyParser from 'body-parser';
//Autor: Yannick Hiltscher 

dotenv.config();
const cors = require('cors');
const app: Express = express();
app.use(cors());
var BodyParser=require('body-parser')

app.use(ArtistRouter);
app.use(infoRouter);
app.use(bodyParser.json())

const port = process.env.PORT;
app.listen(port, () => {
    console.log(`Server started at http://localhost:${port}`);
});

字符串
我的路由器:

ArtistRouter.post('/artist', async (request,response) => {
    var Nation = request.body.Nation;
    var GB = request.body.GB; 
    var FN= request.body.FN;
    var LN=request.body.LN;
    var Web=request.body.Website;
    var Insta=request.body.Insta;
    var Rating=request.body.Rating;
    NewArtists(request, response, Nation, GB, FN, LN, Web, Insta, Rating);


我的下一个js代码:

const ArtistData = {
      "Nation":"Australia",
      "GB":"amogus",
      "FN":"ei9fj",
      "LN":"eifj",
      "Website":"woi",
      "Insta":"fij",
      "Rating":"fjfj",
    }

    const API='http://localhost:8081/artist'
    axios.post(API, ArtistData)
        .then(response => {
           console.log('New artist created:');
           displaySuccessMessage("Artist created");
       })
        .catch(error => {
           console.log(ArtistData)
            console.error('Error creating artist:', error);
            displaySuccessMessage('Error creating artist');
       });
          const A=1;
         return A;
       }


我希望我的下一个js程序给予几个变量给我的API,但我得到错误“跨源请求阻塞”
可悲的是,我不能让它工作,我不知道为什么。有人知道为什么我的NextJS服务器没有权限的职位要求?

i86rm4rw

i86rm4rw1#

试试这个

import express from 'express';
import dotenv from 'dotenv';
import ArtistRouter from './routes/artist_routes';
import infoRouter from './routes/info_router';
import bodyParser from 'body-parser';
//Autor: Yannick Hiltscher 
import cors from 'cors'
dotenv.config();
const app = express();
app.use(bodyParser.json())
app.use(cors());
app.use(ArtistRouter);
app.use(infoRouter);
const port = process.env.PORT || 8081;
app.listen(port, () => {
    console.log(`Server started at http://localhost:${port}`);
});

字符串

相关问题