解决axios post请求发送json数据报错
在使用Axios向/auth/loginByWeb接口发送POST请求时,错误地将数据附加到URL上,导致报错。 正确的做法是将JSON数据作为请求体发送,而不是URL参数。
正确方法:
要正确发送JSON数据,需要:
修改后的代码:
JavaScript (axios请求函数):
// login export const PostLogin = (params) => request.post( `/auth/loginByWeb`, params, { headers: { 'Content-Type': 'application/json' }, } );
Vue.js (调用请求函数):
methods: { async onSubmit() { const logindata = { username: this.username, pwd: this.pwd, }; const { data: res } = await PostLogin(logindata); // 处理返回结果res } }
通过以上修改,Axios将正确地以JSON格式发送登录数据到后端接口。 请确保您的后端接口能够正确解析JSON数据。
以上就是Axios POST请求报错:如何正确发送JSON数据到接口?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号