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 接收就行

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM