前后端数据传输问题:spring/ajax
ajax调用接口415错误
前后端数据传输问题:spring/ajax
ajax调用接口415错误的时候,很可能是你忘记在ajax中加入
1 contentType : "application/json;charset=UTF-8"
下方有完整实例。
问题描述 参数有问题,不能解析成json对象
1 Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'name' : was expecting ('true' , 'false' or 'null' )
解决方案 ajax提交添加下面两行代码
1 2 contentType:'application/json;charset=utf-8' data:JSON.stringify(数据)
补充说明 后端如果用 SpringMVC 的@RequestBody 注解,则只能 JSON 对象的字符串,不能接收 JSON 对象 ,用 JSON.stringify(data) 的方式将对象变成字符串,同时 ajax请求也要指定 dataType: “json”,contentType:”application/json” ,这样就能用@RequestBody注解绑定对象或者List集合.
完整登录实例: 前台:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 <!DOCTYPE html > <html > <head > <title > login.html</title > <meta charset ="utf-8" > </head > <script type ="text/javascript" src ="jquery-3.5.1.min.js" > </script > <script type ="text/javascript" > $(function ( ){ $("#submit" ).click (function ( ){ var url = "http://localhost:8080/api/login" ; var users = {"username" :$("input[type='text']" ).val (), "password" :$("input[type='password']" ).val ()}; $.ajax ({ "url" : url, "data" :users, "type" : "post" , dataType : "json" , data :JSON .stringify (users), contentType : "application/json;charset=UTF-8" , "success" : function (data ) { alert (data.id ) }, "error" : function ( ) { alert ("用户名或者密码错误" ); } }); }); }); </script > <body > <form action ="#" > username:<input type ="text" name ="username" > <br > password:<input type ="password" name ="password" > <br > <input type ="button" value ="submit" id ="submit" > </form > </body > </html >
后台:
LoginController 类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 import com.felixcai.movie.api.domain.User;import com.felixcai.movie.api.repository.LoginRepository;import io.swagger.annotations.Api;import io.swagger.annotations.ApiImplicitParam;import io.swagger.annotations.ApiImplicitParams;import io.swagger.annotations.ApiOperation;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpServletResponse;@CrossOrigin @Api(value="用户登录controller",tags={"用户登录操作接口"}) @Controller @RequestMapping(value = "/api") public class LoginController { @Autowired LoginRepository loginRepository; @RequestMapping(value = "/login",method = RequestMethod.POST) @ResponseBody public User userLogin (HttpServletResponse response, @RequestBody User users) { User user = loginRepository.login(users.getUsername(), users.getPassword()); if (user == null ) { return new User (0 ,"404" ,"404" ); } return user; } }
LoginRepository 类:
1 2 3 4 5 6 7 8 9 10 11 import com.felixcai.movie.api.domain.User;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.data.jpa.repository.JpaSpecificationExecutor;import org.springframework.data.jpa.repository.Query;public interface LoginRepository extends JpaRepository <User,Integer>, JpaSpecificationExecutor<User> { @Query(value = "select * from user where username = ? and password = ?",nativeQuery = true) User login (String username,String password) ; }
User 类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 import io.swagger.annotations.ApiModel;import io.swagger.annotations.ApiModelProperty;import javax.persistence.Entity;import javax.persistence.Id;@Entity public class User { @Id private Integer id; private String username; private String password; public User () { } public User (Integer id, String username, String password) { this .id = id; this .username = username; this .password = password; } public Integer getId () { return id; } public void setId (Integer id) { this .id = id; } public String getUsername () { return username; } public void setUsername (String username) { this .username = username; } public String getPassword () { return password; } public void setPassword (String password) { this .password = password; } }