java - Servlet with Ajax - POST works, but GET does not -
i making application able count numbers written in formulas. in html have put this:
<input type="text" size="20" id="number2" onblur="validate2()" onfocus = "document.getelementbyid('msg2').innerhtml = ' '"> <br> <div id = "message1"> </div>
i have created javascript firstly validating datas , later inserts them 'answer-formula':
function validate2() { var idfield2 = document.getelementbyid("number2"); var data2 = "number2=" + encodeuricomponent(idfield2.value); if (typeof xmlhttprequest != "undefined") { req = new xmlhttprequest(); } else if (window.activexobject) { req = new activexobject("microsoft.xmlhttp"); } var url = "validator" req.open("get", url, true); req.onreadystatechange = inserter2 req.setrequestheader("content-type", "application/x-www-form-urlencoded") ; req.send(data2); } function inserter2() { if (req.readystate == 4) { if (req.status == 200) { var msg1 = req.responsetext if (msg1 == "") { document.getelementbyid("message1").innerhtml = "<div style=\"color:red\">zła nazwa</div>"; document.getelementbyid("org").value = '' } else document.getelementbyid("org").value = msg2 } } }
and here's code in sending doget:
public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { system.out.println("get"); response.setcontenttype("text/html"); writer out = response.getwriter(); string num2 = request.getparameter("number2"); system.out.println(num2); string org = new string(); if(num2.matches("[\\p{punct}\\d]+")) { scriptenginemanager mgr = new scriptenginemanager(); scriptengine engine = mgr.getenginebyname("javascript"); try { org = engine.eval(num2).tostring() + " (" + request.getmethod() + ")"; } catch (exception e) { e.printstacktrace(); } } out.write(org != null ? org : ""); }
if change these things post, code work, get, doesn't work @ all. strange situation formula post can read written things in formula, using get, program see formula source null.
if send get, need put formula request. expression:
req.send(data2);
doesn't make sense when send request, request cannot contain payload except in request string. have add payload url. this:
var url = "validator/?" + data2; req.open("get", url, true); req.onreadystatechange = inserter2; req.send();
hope helps.
Comments
Post a Comment