Sujan
Sujan

Reputation: 1562

How can I get data from spring controller by ajax?

I have an ajax on a jsp page which calls a spring controller through URL /check .

$.ajax({
    type : "GET",
    url : "${pageContext.request.contextPath}/check",
    data : {
    "id" : ${articleCount}
    },
    success: function(data){
    //response from controller
    }
});

Now, the controller looks like,

@RequestMapping("/check")
public String check(@RequestParam Integer id, HttpServletRequest request,
        HttpServletResponse response, Model model) {
    boolean a = getSomeResult();
    if (a == true) {
        model.addAttribute("alreadySaved", true);
        return view;
    } else
        model.addAttribute("alreadySaved", false);

    return view;
}

I sent data using model and tried to access it in success: function(data) as "${alreadySaved}"but it shows blank.

Is there any way I can receive that true/false data on the view page?

Upvotes: 11

Views: 64583

Answers (4)

Du-Lacoste
Du-Lacoste

Reputation: 12757

use @ResponseBody

Spring will bind the return value to outgoing HTTP response body when you add @ResponseBody annotation.

@ResponseBody
public String check(@RequestParam Integer id, HttpServletRequest request, HttpServletResponse response, Model model) {
    boolean a = getSomeResult();
    if (a == true) {
        return "already saved";
    } 
    return "error exist";
}

Spring will use HTTP Message converters to convert the return value to HTTP response body [serialize the object to response body], based on Content-Type used in request HTTP header.

for more info:

http://websystique.com/springmvc/spring-mvc-requestbody-responsebody-example/

Upvotes: 4

Selva
Selva

Reputation: 1670

You have to add the @ResponseBody annotation for spring ajax calls example

@RequestMapping("/check")     
@ResponseBody
public String check(@RequestParam Integer id, HttpServletRequest request, HttpServletResponse response, Model model) {
    boolean a = getSomeResult();
    if (a == true) {
        model.addAttribute("alreadySaved", true);
        return view;
    } else {
        model.addAttribute("alreadySaved", false);
        return view;
    }
}

Upvotes: 10

Santhosh
Santhosh

Reputation: 8187

When you are trying to return the values from the ajax request , you should use the @ResponseBody annotation . As your method return type is String make sure you return the string value and not your view to the jsp.

Since it will render the jsp again the response

@ResponseBody
public String check(@RequestParam Integer id, HttpServletRequest request, HttpServletResponse response, Model model) {
    boolean a = getSomeResult();
    if (a == true) {
        return "already saved";
    } 
    return "error exist";
}

And in your jsp,

success: function(data){
    alert(data);
}

Upvotes: 1

tofindabhishek
tofindabhishek

Reputation: 121

Controller part

You have to add the @ResponseBody annotation for spring ajax calls example

View Part

$.ajax({
    type : "GET",
    url : "${pageContext.request.contextPath}/check",
    data : {
        "id" : ${articleCount}
    },
    success: function(data){
        $('#input_field').val(data);
    }
});

Upvotes: 1

Related Questions