java - Send javascript variables to spring controller -


i'm trying develop simple application using spring mvc , need pass javascript parameters spring controller. have tried several methods , none of them worked. following javascript , spring controller. please me sort 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 ajax"+ id);         return "hello";      } } 

when pass json requestbody above call applicable. send request param have 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 can acheive using method 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);  }  } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -