如何从JQuery Ajax调用中调用Spring MVC控制器 [英] How To Call Spring MVC Controller From JQuery Ajax Call

查看:183
本文介绍了如何从JQuery Ajax调用中调用Spring MVC控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从JQuery ajax调用中调用Spring控制器方法,但它无法导航到相应的视图.

首先,我要通过从ajax调用调用authenticateLogin()Spring控制器函数来验证登录详细信息,成功验证后,我需要将请求转发到相应的视图页面,我尝试使用以下代码,但无法导航到另一页面. /p>

JavaScript函数:

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";
}

解决方案

您好,我没有注释权限,所以只需回答您的问题即可.如果注释数据部分是GET,请键入注释并将其从Java端删除. @ModelAttribute("loginDO") Login login,否则只需将其设为POST,然后检查是否存在CSRF令牌以确保安全.

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.

这篇关于如何从JQuery Ajax调用中调用Spring MVC控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆