鍍金池/ 問答/Java  HTML/ Ajax訪問SpringMVC的Controller進(jìn)入error

Ajax訪問SpringMVC的Controller進(jìn)入error

Ajax訪問SpringMVC的Controller進(jìn)入error

##* JSP
<form action="#" method="post" id="loginform" onsubmit="return false">
        <input type="text" id="username" name="username" class="name" placeholder="username" required="">
        <input type="password" id="password" name="password" class="password" placeholder="password" required="">
        <span id="msg" style="color:#F00;font-size:14px;"></span><br><br>
        <ul>
            <li>
                <input type="checkbox" id="brand1" value="">
                <label for="brand1"><span></span>記得我</label>
            </li>
        </ul>
        <a href="#">忘記密碼?
        </a><br>
        <div class="clear"></div>
        <input type="button" id="submit" value="Login" onclick="login()">
    </form>
    
##*Ajax
<script type="text/javascript">
        function login(that) {
            $.ajax({
                data: "username=" + $("#username").val() + "&password=" + $("#password").val(),
                type: "post",
                url: "/checklogin",
                dataType: "json",
                error: function (data) {
                    alert("出現(xiàn)異常,請(qǐng)稍后重試");
                    $(that).removeClass("processing");
                },
                success: function (response) {
                    $(that).removeClass("processing");
                    if (response == "error") {
                        $("#msg").text("用戶名或密碼錯(cuò)誤");
                    } else {
                        window.location.href = "/welcome";
                    }
                }
            });
        }
</script>

##Controller
@Controller
public class LoginController {
    @Autowired
    private UserService userService;
    
    @RequestMapping("/checklogin")
    @ResponseBody
    public String checkLogin(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session){
        User user = userService.login(username, password);
        if(user != null){
            return "success";
        }
        else{
            return "error";
        }
    }
    
}

error返回(object object)

剛剛學(xué)習(xí)虛心請(qǐng)教

回答
編輯回答
掛念你

dataType: "object" 更改為dataType: "json"

ajax的返回類型中沒有object這個(gè)類型的

2017年5月13日 20:30
編輯回答
柒槿年

去掉@resposebody?

2017年3月14日 11:54