javascript - HttpServletRequest can not get xhr request parameter -
i working spring , javascript. calling @controller
xhrhttprequest
object.
i can see parameter(json string) chrome inspector, when call request.getparamter("id")
returns null.
calling part js function ajax(url, data, callback, method){ //data {"id":"system", "password" : "1234"} var httprequest; var afteraction = function(){ if(!httprequest) { console.error('can not find httprequest variable'); return; } if (httprequest.readystate === xmlhttprequest.done) { if (httprequest.status === 200) { var responsedata = httprequest.responsetext; //alert(json.stringify(responsedata)); console.log('result of api call >>>', responsedata); if(typeof callback == 'function') { callback(json.parse(responsedata)); } } else { alert('there problem request.'); } } } //=========== logic ============ if (window.xmlhttprequest) { // mozilla, safari, ie7+ ... httprequest = new xmlhttprequest(); } else if (window.activexobject) { // ie 6 , older httprequest = new activexobject('microsoft.xmlhttp'); } if(!method) method = 'post'; data = (!!data) ? json.stringify(data) : ''; httprequest.onreadystatechange = afteraction; httprequest.open(method.touppercase(), url, true); httprequest.setrequestheader("content-type", "application/json; charset=utf-8"); //httprequest.setrequestheader("content-length", data.length); //httprequest.setrequestheader("connection", "close"); httprequest.send(data); }
receive part spring @controller
@controller @requestmapping(value={"member"}, produces={"application/json"}) @responsestatus(value = httpstatus.ok) public class membercontroller { /** * @param request * @param resp * @return * @throws exception */ @requestmapping(value={"/login"}) public @responsebody string login(httpservletrequest request, httpservletresponse resp) throws exception { system.out.println("login request"); string id = string.valueof(request.getparameter("id")); //returns null string password = string.valueof(request.getparameter("password")); //returns null map<string, string> result = new hashmap<string, string>(); result.put("result", "s"); result.put("message", "login success"); objectmapper mapper = new objectmapper(); return mapper.writevalueasstring(result); } }
i not know why parameter becomes null. thanks.
you need follow below steps accept json
in controller:
(1) define userlogin bean hold json
public class userlogin { private string id; private string password; //add getters , setters }
(2) change controller accept json & receive userlogin bean
@controller @requestmapping(value={"member"}, produces={"application/json"}) @responsestatus(value = httpstatus.ok) public class membercontroller { @requestmapping(value={"/login"}, method = requestmethod.post, consumes = mediatype.application_json_value) public @responsebody string login(userlogin userlogin) throws exception { system.out.println("login request"); string id = userlogin.getid(); string password = userlogin.getpassword(); map<string, string> result = new hashmap<string, string>(); result.put("result", "s"); result.put("message", "login success"); objectmapper mapper = new objectmapper(); return mapper.writevalueasstring(result); } }
Comments
Post a Comment