java ajax controller,如何从JQuery Ajax调用中调用Spring MVC控制器

i am attempting to call Spring controller method from JQuery ajax call, but its not navigate to corresponding view.

First i am verifying login details by calling authenticateLogin() Spring controller function from ajax call, after successful validate i need to forward the request to corresponding view page, i have tried with below code but its not navigate to another page.

Javascript function:

function authenticatePricingCalcLogin() {

var login = {

userName : $("#username").val(),

password : $("#password").val()

};

$.ajax({type: "POST",

url: CONTEXT_PATH+"authenticateLogin",

data:JSON.stringify(login),

contentType : 'application/json; charset=utf-8',

dataType : 'json',

success: function (response) {

if (response != null) {

if (response.errorMsg != null && response.errorMsg != "") { // Login Error

alert(response.errorMsg);

} else {

// Here i need to call spring controller method and to redirect to another page

// I have tried

$.ajax({type: "GET",

url: CONTEXT_PATH+"navigateMainPage",

data:JSON.stringify(loginDO),

contentType : 'application/json; charset=utf-8',

dataType : 'json'

});

}

}

}

});

}

AuthController.java

@RequestMapping(value = "/authenticateLogin", method = RequestMethod.POST)

public @ResponseBody LoginDO authenticateLogin(@RequestBody Login login){

return authService.authenticateLogin(loginDO);

}

@RequestMapping(value = "/navigateMainPage", method = RequestMethod.GET)

public String navigateMainPage(@ModelAttribute("loginDO") Login login,HttpServletRequest request, Model model) {

try {

// Need to set User Name in session variable

} catch (Exception e) {

}

return "auth/mainPage";

}

解决方案

Hi friend I don't have Comment authority so just answering your question.just comment data part if it is GET Type request and remove it form java side @ModelAttribute("loginDO") Login login, Otherwise just make it POST and check any CSRF token is there or not for safe side.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值