1. axiox的post参数提交

  1. axios.post默认是application/json方式提交数据,不是表单
  2. ajax中的post默认是表单的方式提交
  3. psot表单方式提交 application/x-www-form-urlencoded 采用&拼接

2. 修改axios post的提交方式为表单的方式

  1. axios.post方法 默认发送数据格式
  2. application/json的方式发送的
  3. {"name":"david","age":30}
  4. axios({
  5. url:"地址",
  6. method:"post",
  7. data:{
  8. name:"david",
  9. age:30
  10. },
  11. transformRequest: [function (data) {
  12. // Do whatever you want to transform the data
  13. let ret = ''
  14. for (let it in data) {
  15. // 如果要发送中文 编码
  16. ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
  17. }
  18. return ret
  19. }],
  20. headers: {
  21. 'Content-Type':'application/x-www-form-urlencoded'
  22. }
  23. }).then(res=>{
  24. console.log(res)
  25. })
  26. 发送的数据就是跟表单一样
  27. application/x-www-form-urlencoded
  28. name=david&age=30

3. 案例

html

  1. <template>
  2. <div id="app">
  3. ![](./assets/logo.png)
  4. <h1>用户登录</h1>
  5. <form>
  6. <input type="text" id="name" placeholder="用户名"><br>
  7. <input type="password" id="password" placeholder="密码"><br>
  8. <input type="submit" @click.prevent="loginUser" value="提交">
  9. </form>
  10. <hr>
  11. <h2>用户注册</h2>
  12. <user-info></user-info>
  13. </div>
  14. </template>

js

  1. <script>
  2. import axios from "axios";
  3. export default {
  4. name: 'app',
  5. data () {
  6. return {
  7. msg: 'Welcome to Your Vue.js App'
  8. }
  9. },
  10. components:{
  11. "user-info":{
  12. data(){
  13. return {
  14. name:"",
  15. password:"",
  16. email:""
  17. }
  18. },
  19. methods:{
  20. registUser(){
  21. //注册用户信息
  22. var self = this;
  23. // 默认是applicatino/json方式提交数据 不是表单
  24. // 下面这个代码是修改post的提交方式为表单的方式
  25. /*
  26. axios.post方法 默认发送数据格式
  27. application/json的方式发送的
  28. {"name":"david","age":30}
  29. axios({
  30. url:"地址",
  31. method:"post",
  32. data:{
  33. name:"david",
  34. age:30s
  35. },
  36. transformRequest: [function (data) {
  37. // Do whatever you want to transform the data
  38. let ret = ''
  39. for (let it in data) {
  40. // 如果要发送中文 编码
  41. ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
  42. }
  43. return ret
  44. }],
  45. headers: {
  46. 'Content-Type':'application/x-www-form-urlencoded'
  47. }
  48. })
  49. 发送的数据就是跟表单一样
  50. application/x-www-form-urlencoded
  51. name=david&age=30
  52. */
  53. axios(
  54. {
  55. url:'http://localhost:8000/myRegist',
  56. method:"post",
  57. data:{
  58. name:self.name,
  59. password:self.password,
  60. email:self.email
  61. },
  62. //将JSON对象 转 键=值&键=值
  63. /*
  64. {
  65. name:"david",
  66. age:30
  67. }
  68. name=小李&age=30
  69. // 在发送数据之前 将对象转键值对
  70. */
  71. transformRequest: [function (data) {
  72. // Do whatever you want to transform the data
  73. let ret = ''
  74. for (let it in data) {
  75. // 如果要发送中文 编码
  76. ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
  77. }
  78. return ret
  79. }],
  80. headers: {
  81. 'Content-Type':'application/x-www-form-urlencoded'
  82. }
  83. })
  84. .then(function (response) {
  85. console.log(response);
  86. })
  87. .catch(function (error) {
  88. console.log(error);
  89. });
  90. }
  91. }
  92. }
  93. },
  94. methods:{
  95. loginUser(){
  96. // 发送数据给服务器
  97. //http://localhost:8000/myLogin?name=david&password=123
  98. /*
  99. localhost/:1 XMLHttpRequest cannot load http://localhost:8000/myLogin?name=wang&password=dfasf. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
  100. */
  101. console.log("发送数据");
  102. var name = document.getElementById("name").value;
  103. var password = document.getElementById("password").value;
  104. var path = "http://192.168.104.31:8000/myLogin";
  105. axios.get(path,{
  106. params:{
  107. name,
  108. password
  109. }
  110. }).then(function(data){
  111. // 验证成功和失败 怎么知道成功了还是失败了
  112. console.log(data);
  113. if(data.data.code==0){
  114. alert("登录成功");
  115. }else {
  116. alert("登录失败");
  117. }
  118. },function(err) {
  119. console.log(err);
  120. }).catch(function(err){
  121. console.log("捕获错误");
  122. })
  123. }
  124. }
  125. }
  126. </script>