繁体   English   中英

Ajax不调用控制器方法

[英]Ajax not calling controller method

我是Ajax的新手。 我试图调用我的控制器方法,但是从Ajax调用而不调用我给出的URL。在这里,我的控制器方法和ajax调用在我的jsp文件中。

@RequestMapping(value = "/getdata", method = RequestMethod.POST)
public @ResponseBody String add(@RequestParam(value="userDetailObject", required=true) User_Details u,Model model) {
    System.out.println("In the controller method..");
    String result=null;
                result="From controller :"+u.getEmail();
                System.out.println("at controller .."+result);
    return result;
}

而我的Ajax是

//script type="text/javascript" src="../jquery-1.4.4.js"

var jq = jQuery.noConflict();

        function getData() {
            alert("hello");
            // get the form values
        var email = $('#email').val();
        //  var education = $('#education').val();
            $.ajax({
                type : 'POST',
                dataType: 'json',
                url : "/SpringDemoSts/getdata",
                data : 'email=' + email,
                success : function(response) {
                    // we have the response
                    //$('#info').html(response);
                    $('#email').val(response);
                    //$('#education').val('');
                },
                error : function(e) {
                    alert('Error: ' + e);
                }
            });
        }
/script

我在这里做错了什么事?

在ajax中定义的URL:

/SpringDemoSts/getdata

与显示的请求映射中的不同

/getdata

并且参数名为userDetailObject,并且您的ajax调用错误地定义了数据,您正在使用请求参数样式作为数据。 实际上,您需要json /一个简单的原语或字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM