Basic codes (springboot + html + vue)
Background of the front end login request:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>测试contenttype</title> <link type="text/css" href="/test.css" rel="stylesheet"></link> <style> /*body{*/ /* background-color: #2b542c;*/ /*}*/ </style> </head> <body> <div id="app"> <input type="text" name="username" placeholder="请输入用户名" v-model="formData.username"/> <button @click='submit'>按钮</button> </div> <script src="/js/jquery.min.js"></script> <script src="/js/vue.js"></script> <script>var app = new Vue({ el:'#app' Formdat to {, data:{ username:'你好啊,vue', }, }, methods:{ submit() { alert(this.formData.username); var data = JSON.stringify(this.formData); console.log(data); $.ajax({ url: '/login', data: data, type: 'POST', contentType: 'file application / JSON; charset = UTF-. 8 },' , // background with @requestBody data acquisition and receiving objects // contentType:' ', dataType: ' JSON ' , // Cache: false, processData: false , // tell jQuery not to handle data sent // contentType: to false, // tell jQuery not to set the request Content-Type header Success: function (Data) { the console.log ( ' return value ' + Data) error: function (Data) { the console.log (' Request failed " ); } }) } } }); </ Script> </ body> </ HTML>
Background controller
package com.lizhi.login.controller; import com.lizhi.login.entity.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller //@ResponseBody public class LoginController { @RequestMapping({"","/"}) String welcome(){ return "test"; } @RequestMapping(value = "/login") public void login(@RequestBody User user){ System.out.println("用户名:"+user); // System.out.println("密码:"+psd); System.out.println("请求成功"); } }
Scenario 1
前端:contentType: 'application/json;charset=UTF-8',
Scenario 2
Distal: contentType: 'file application / X-WWW-form-urlencoded', // form imitating the form submission