[axios]"Content-Type": "application/x-www-form-urlencoded" 携带多余字符:

2023-12-11 402 views
6
Describe the issue

"Content-Type": "application/x-www-form-urlencoded" 是 会携带 一个 :

Example Code

Code snippet to illustrate your question

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
        <style>
            .uploadWrap {
                margin: 20px;
            }
        </style>
    </head>
    <body>
        <div>

            <button onclick="axiosPostWithForm()">axios post 提交json格式数据</button>

        </div>

    </body>

    <script>
        const BASEURL = "http://localhost:7001/api";

        // axios post 提交application/x-www-form-urlencoded格式数据
        function axiosPostWithForm() {
            axios({
                url: BASEURL,
                method: "post",
                data: {
                    id: 121,
                },
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded",
                },
            }).then(res => {
                console.log(`axios request`);
                console.log(res.data);
            });
        }
    </script>
</html>
Expected behavior, if applicable

{"id":121}

Environment
  • Axios Version [e.g. 0.18.0]
  • Adapter [e.g. XHR/HTTP]
  • Browser [e.g. Chrome, Safari]
  • Browser Version [e.g. 22]
  • Node.js Version [e.g. 13.0.1]
  • OS: [e.g. iOS 12.1.0, OSX 10.13.4]
  • Additional Library Versions [e.g. React 16.7, React Native 0.58.0]
Additional context/Screenshots

image

回答

6

@2462870727 看起来是chrome的问题,Safari上面似乎没有这个问题?

image

chrome上显示源代码也是正常的: image

1

这种方式传递似乎不是合法的, 下面这种方式 能够正常传递过去

axios({
                url: BASEURL,
                method: "post",
                data: 'id=121&name=jack',
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded",
                },
            }).then(res => {
                console.log(`axios request`);
                console.log(res.data);
            });