axios发送post请求后台接受不到问题

axios发送post请求后台接受不到问题

1、首先这是前端的问题

2、解决方案不唯一,但这招肯定行

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.js"></script>
</head>

<body>
    <input type="button" onclick="sendHttp()" value="发送POST请求">
</body>

<script>
    function sendHttp() {
        // 传统jquery post
        // $.post('http://module.design.xesimg.com/xeditor/textToFile', {
        //     "main": "sadfasdf",
        //     "resource": "qwerqwer",
        //     "moduleConfig": "glhfkjd"
        // }, (msg) => {
        //     console.log(msg)
        // })

        //axios post
        var params = new URLSearchParams();
        params.append('main', 'value1'); //你要传给后台的参数值 key/value
        params.append('resource', 'value2');
        params.append('moduleConfig', 'value3');
        axios.post('http://127.0.0.1:3000/xeditor/textToFile', params)
            .then(function (response) {
                console.log(response);
            })
            .catch(function (error) {
                console.log(error);
            });
    }
</script>

</html>

node.js 后端用 req.body 接收就行

原文地址:https://www.cnblogs.com/xbblogs/p/9562171.html