为什么在删除时出现CORS错误,而在发布时却没有?

时间:2019-05-09 08:24:32

标签: javascript node.js reactjs express cors

我正在对express和node.js应用程序做出反应,我在删除时遇到CORS错误,但在发布/获取时却没有... 这是node.js代码:

app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
    bodyParser.json()
    next();
});

app.delete("api/delete/item", (req, res) => {
    console.log(req.body.id)
})

和React部分:

import React from 'react'
import { Button } from 'reactstrap'
import axios from 'axios'
const Delete = props => {
    const id = props.id
    const remove = () => {
        axios.delete('http://localhost:5000/api/delete/item', { id: id})
            .then(res => console.log(res))
    }
    return (
        <Button onClick={() => remove()} >Delete</Button>
    )
}

export default Delete

我忘记了实际的错误:

localhost/:1 Access to XMLHttpRequest at 'http://localhost:5000/api/delete/item' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

0 个答案:

没有答案