甜品屋 2020-04-14 14:30 采纳率: 0%
浏览 646

vue 里面 fetch的delete方法请求参数不成功,但是用get可以成功

这个是我的js文件

const express = require('express')
const app = express()
const bodyParser = require('body-parser')
app.use(express.static('public'))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}))

app.all('*',function (req,res,next) {
    res.header("Access-Control-Allow-Origin","*");
    res.header("Access-Control-Allow-Methods","PUT, GET, POST, DELETE, OPTIONS");
    res.header("Access-Control-Allow-Methods","X-Requested-With");
    res.header("Access-Control-Allow-Methods","Content-Type");
    next();
});
//fetch传统方式
app.get('/fetchdata',(req,res)=>{
    res.send('传统方法获取参数'+req.query.id)
})
// fetch第二种方式
app.get('/fetchdata/:id',(req,res)=>{
    res.send('2方法获取参数'+req.params.id)
})
app.delete('/fetchdata/:id',(req,res)=>{
    res.send('delete方法获取参数'+req.params.id)
})
app.post('/fetchdata',(req,res)=>{
    res.send('dpost方法获取参数'+req.body.username+'----------'+req.body.password)
})
app.get('/a1',(req,res)=>{
    setTimeout(function(){
        res.send('a1')
    },1000);
})
app.get('/a2',(req,res)=>{
    setTimeout(function(){
        res.send('a2')
    },2000);
})
app.get('/a3',(req,res)=>{
    setTimeout(function(){
        res.send('a3')
    },3000);
})
app.get('/data',(req,res)=>{
    res.send('Hello World')
})
app.get('/data1',(req,res)=>{
    res.send('Hello tom')
})
app.get('/data2',(req,res)=>{
    res.send('Hello jerry')
})
app.listen('3000',()=>{
    console.log("running")
})

这个是我的html文件,用来请求js数据,用get方法可以成功

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="vue.js"></script>
    <title>Document</title>
</head>
<body>
    <script>
        // fetch('http://localhost:3000/a1').then(function (data) {
        //     //这个必须的,用于获取后台对象
        //     return data.text();

        // }).then(function (data) {
        //     console.log(data);
        // })


        // fetch传参
        // fetch('http://localhost:3000/fetchdata?id=156', {
        //     method:'get'
        // }).then(function (data) {
        //     return data.text();
        // })
        // .then(function (data) {
        //     console.log(data);
        // })
        //第二种方式(这里用get方法可以成功的)
        // fetch('http://localhost:3000/fetchdata/456', {
        //     method:'get'
        // }).then(function (data) {
        //     return data.text();
        // })
        // .then(function (data) {
        //     console.log(data);
        // })
                //这里用delete方法就失败了
        fetch('http://localhost:3000/fetchdata/741', {
            method: 'delete'
        }).then(function (data) {
            return data.text();
        })
        .then(function (data) {
            console.log(data);
        })
    </script>
</body>
</html>

这个是报错的信息
Failed to load http://localhost:3000/fetchdata/741: Method DELETE is not allowed by Access-Control-Allow-Methods in preflight response.

  • 写回答

2条回答 默认 最新

  • 关注

    同样遇到这个问题,请问你解决了吗

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算