本人刚开始接触springmvc,项目搭建参照https://my.oschina.net/gaussik/blog/385697。在用IDEA写登录注册的时候,想通过ajax方式提交数据到后台,然后遇到如题所述的乱码问题,然后度娘了好多,终于解决了。废话不多说,直接上代码。

首先是登录页面login.jsp

 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: PENG027
 4   Date: 2016/11/11
 5   Time: 15:48
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" pageEncoding="utf-8" language="java" %>
 9 <html>
10 <head>
11     <title>index</title>
12     <script type="text/javascript" src="/js/jquery.min.js"></script>
13     <script type="text/javascript">
14         function login() {
15             var name=$("#id1").val();
16             var pwd=$("#id2").val();
17             $.ajax({
18                 url:'/login',
19                 type:'post',
20                 contentType:'application/json',
21                 data:JSON.stringify({'username':name,'password':pwd}),
22                 dataType:'json',
23                 async:false,
24                 success:function (msg) {
25 //                    alert("页面内容改变");
26 //                    $("#p").text(msg.msg);
27                     window.location.href="success.jsp?>Math.random();
28                 },
29                 error:function () {
30                     alert("failure");
31                 }
32             });
33         }
34     </script>
35 </head>
36 <body>
37 <form>
38     username:<input /><br>
39     password:<input /><br>
40     <input /><br>
41     <%--<p >点击登录之后我会改变</p>--%>
42 </form>
43 </body>
44 </html>
View Code

相关文章:

  • 2022-12-23
  • 2021-05-22
  • 2021-09-12
  • 2022-12-23
  • 2022-12-23
  • 2022-02-09
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-28
  • 2021-11-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-04
相关资源
相似解决方案