Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

使用SpringMVC的controller中能获取数据但直接跳到异常页面,使用Ajax。

Controller代码

import java.util.Date;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.bdqn.springmvc.po.User;
import com.alibaba.fastjson.JSON;

@Controller
public class DemoController {

@RequestMapping("/ajaxGetUser")

@ResponseBody

public String ajaxGetUser(){

    User user = new User();
    user.setUserName("诸葛七");
    user.setAge(2);
    user.setPwd("666");
    user.setRagDate(new Date());
    String json = JSON.toJSONString(user);
    return json;
}

}
###### jsp代码
<html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<script src="<%=request.getContextPath()%>/static/js/jquery-1.8.3.js"></script>
<script>

  $(function () {
    $.ajax({
      url:"<%=request.getContextPath()%>/ajaxGetUser",
      type:"post",
      dataType:"json",
      success: function (data) {
        alert(data);
        //直接加载

var result = "";

        if (data != null){
          result = result +"<p>"+ data.userName +"</p>"

+"<p>"+ data.pwd +"</p>"
+"<p>"+ data.age +"</p>"
+"<p>"+ data.ragDate +"</p>";

        }
        $("#uaerInfo").html(result)
      },
      error:function (err) {
        alert(err);
      }
    })
  })

</script>
<body>
<h2>Hello World!</h2>
<img src="static/img/001.png">
<div id="uaerInfo">
</div>
</body>
</html>
不知道哪里错了,运行后会直接跳到异常页面


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...