Ajax没有调用控制器方法

时间:2013-03-01 12:39:01

标签: javascript jquery ajax spring spring-mvc

我是Ajax的新手。我试图调用我的控制器方法,但从Ajax调用不调用url我得到..这里我的控制器方法和我的jsp文件中的ajax调用..

@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

我错在哪里错了?

1 个答案:

答案 0 :(得分:0)

在ajax中定义的网址:

/SpringDemoSts/getdata

在显示的请求映射中不一致

/getdata

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

相关问题