java - how can I send and retrieve params in spring? -


i triyng simple thing, ajax, send request (using get, or post).

i sending 2 parameters in json format , , want them , send response, still, error 400 , others dont know whats wrong, idea how?

i started based on article: http://fruzenshtein.com/spring-mvc-ajax-jquery/

i using spring mvc.

so far have this:

$(".update_agent").live('click', function(){              var agent = { "agentid" : agentid, "hostagent" : hostid};             //send ajax             $.ajax({                 url: url,                 data: json.stringify(agent),                 type: "get",                 beforesend: function(xhr) {                     xhr.setrequestheader("accept", "application/json");                     xhr.setrequestheader("content-type", "application/json");                 },                 success: function(data) {                     alert("success");                 },                 error: function(){                     alert("error");                 }             });         }) 

and @ java controller have this

@requestmapping(value = "/update", method = requestmethod.get, produces = mediatype.application_json_value, consumes = mediatype.application_json_value) @responsebody public int updateagent(httpservletrequest req, httpservletresponse res) throws ioexception{     req.getparametervalues("agentid");     return agent_update_success; } 

but cant back, have no idea how make request of params, idea?

thanks.

=====================update============================

ive changed code , how looks like...

$.ajax({         headers: {         "accept": "application/json",         "content-type": "application/json"         },         type: 'post',         url: url,         data: json.stringify(agent),         datatype: 'json',         success:function(data) {            alert("success");         },         error: function(){             alert("error");         } }); 

and @ controller

@requestmapping(value = "/update", method = requestmethod.post)     public @responsebody integer  updateagent(@requestbody string param) throws ioexception{         system.out.println(param);         //do something...         return 1;      } 

the problem getting error 415, unsupported media type, advice?

get-request can not have 'data'-field. need send data part of url:

$.ajax({         url: url + "?agent=" + json.stringify(agent),         type: "get",         beforesend: function(xhr) {             xhr.setrequestheader("accept", "application/json");             xhr.setrequestheader("content-type", "application/json");         },         success: function(data) {             alert("success");         },         error: function(){             alert("error");         }     }); 

now can data in controller as:

@responsebody public responseentity<string> updateagent(@requestparam(value = "agent") string agentjson){ ... } 

or can send post-request. post-request can send data requestbody:

public @responsebody responseentity<string> updateagent(@requestbody string agentjson){ ... } 

edit: create new agent-class:

public class agent {     private long agentid;     private long hostagent;     ...     getter , setter     ... } 

now update controller to:

public @responsebody responseentity<string> updateagent(@requestbody agent agent){     system.out.println(agent.getagentid()); } 

and change "content-type" of ajax-call "application/json".

edit2: change ajax-call data to:

data: { agentid: agentid, hostagent : hostagentid} , 

or even

data: agent , 

don't forget change "hostagent" "hostagent" in agent object, or 400!!!

now ajax send data request parameters, can data in controller by:

public @responsebody responseentity<string> updateagent(@requestparam(value = "agentid") long agentid, @requestparam(value = "hostagent") long hostagentid){     system.out.println(agentid); } 

Comments