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

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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题