2

I'm trying to develop simple application using spring mvc and I need to pass javascript parameters to spring controller. I have tried several methods and none of them were worked. Following is my javascript and spring controller. Please help me to sort this issue.

Java script

function searchViaAjax(id) {
    alert(id);
    $.ajax({
        type : "POST",
        contentType : "application/json",
        url : "search/api/getSearchResult",
        data : JSON.stringify(id),
        dataType : 'json',
        timeout : 100000,
        success : function(id) {
            console.log("SUCCESS: ", id);
            display(id);
            alert(response);   
        },
        error : function(e) {
            console.log("ERROR: ", e);
            display(e);
        },
        done : function(e) {
            console.log("DONE");
        }
    });
}

AjaxController.java

@Controller
public class AjaxController {
    @ResponseBody
    @RequestMapping(value = "/search/api/getSearchResult")

    public String getSearchResultViaAjax(@RequestParam(value = "id") int id) {
        System.out.println("come to ajax"+ id);
        return "hello";

    }
}
2
  • data : JSON.stringify({ id: id }), Commented Aug 21, 2017 at 7:10
  • I tried in this way. But didn't work for me Commented Aug 21, 2017 at 7:24

3 Answers 3

3

When you pass json as requestbody then above call is applicable. To send request param you have to as below:

Use following ajax call:

function searchViaAjax(id) {
var tempId = id;
$.ajax({
    type : "POST",
    url : "/search/api/getSearchResult",
    data : {id:tempId},
    timeout : 100000,
    success : function(id) {
        console.log("SUCCESS: ", id);
        display(id);
        alert(response);   
    },
    error : function(e) {
        console.log("ERROR: ", e);
        display(e);
    },
    done : function(e) {
        console.log("DONE");
    }
});
}

also you can acheive this using get method as below:

 function searchViaAjax(id) { 
 $.ajax({ 
 type : "GET", 
 url : "/search/api/getSearchResult/"+id, 
 timeout : 100000, 
 success : function(id) { 
 console.log("SUCCESS: ", id); 
 display(id); 
 alert(response); 
}, 
error : function(e) { 
console.log("ERROR: ", e); 
display(e); 
}, 
done : function(e) { 
console.log("DONE"); 
} 
}); 
} 

@Controller 
public class AjaxController { 

@ResponseBody 
@RequestMapping(value = "/search/api/getSearchResult/{id}") 
public String getSearchResultViaAjax(@PathVariable(value = "id") Integer id) 
{ 
 return String.valueOf(id); 
} 
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for the comment. But this didn't work for me.
change url to url : "/search/api/getSearchResult",
and it returns hello as response not id as you returned "hello" string
'/' in start should add as it append this path to context path of application
I have checked and it is not working for me. It is really simple issue,but i have no idea why this is not working.
|
2

When you use JSON.stringify, you are actually sending a JSON object to your spring controller's method. All you have to do is wrap your json object as a java object like so.

public class UserId {

    private int id;

    // setters and getters

}

And in your controller's method use @RequestBody to map your JSON to your UserId class like so

@ResponseBody
@RequestMapping(value = "/search/api/getSearchResult", method = RequestMethod.POST)
public String getSearchResultViaAjax(@RequestBody UserId user) {
    System.out.println("come to ajax" + user.getId());
    return "hello";
}

PS: Haven't tested it but should work.

Comments

0

It is a post request you are making from your ajax to controller. You are missing "methodType=RequestMethod.POST" on your spring controller.

Replace @RequestMapping(value = "/search/api/getSearchResult") with

@RequestMapping(value = "/search/api/getSearchResult", methodType=RequestMethod.POST)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.