React 轴 post 请求 returns 空数据对象但 POSTMAN returns 数据

React axis post request returns empty data object but POSTMAN returns data

我的axios请求是这样构成的:

    const query = JSON.stringify({'username': 'testuser'})
    const headers = {'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded'}

    useEffect( () => {
        axios({
            method: 'POST',
            headers: headers,
            url: 'http://54.158.146.220/FetchSongs.php',
            data: query
        })
            .then(response => {
                console.log(response)
            })
    }, [url])

但是提交这个请求给我的响应 200 没有数据,即使当我用邮递员发送它时它显示了数据。

Content-Typeapplication/x-www-form-urlencoded 时,你没有在正文中得到任何东西,因为你没有正确传递数据,数据(你的 query 变量)应该不是 JSON 格式。

application/x-www-form-urlencoded: the keys and values are encoded in key-value tuples separated by & with a = between the key and the value. Non-alphanumeric characters in both keys and values are percent encoded: this is the reason why this type is not suitable to use with binary data (use multipart/form-data instead)

From MDN - POST

要以正确的格式传递数据,您可以使用 URLSearchParams