java - How to Send Data from Ajax to Servlets -


servlet code

   protected void doget(httpservletrequest request, httpservletresponse response)         throws servletexception, ioexception {     response.setcontenttype("text/html");     string s =  request.getattribute("stopname").tostring();     response.getwriter().write(s); } 

ajax code

function makerequest(i) {                  var stopname = document.getelementbyid('newstopname' + i).value;                 var longitude = document.getelementbyid('newlongitude' + i).value;                 var latitude = document.getelementbyid('newlatitude' + i).value;                 var description = document.getelementbyid('newstopdesc' + i).value;                  document.getelementbyid('hidnewstopname' + i).value = stopname;                 document.getelementbyid('hidnewlongitude' + i).value = longitude;                 document.getelementbyid('hidnewlatitude' + i).value = latitude;                 document.getelementbyid('hidnewstopdesc' + i).value = description;                 var xmlhttprequest = getxmlhttprequest();                  xmlhttprequest.onreadystatechange = getreadystatehandler(xmlhttprequest);                 xmlhttprequest.open("get", "edit_route", true);                 xmlhttprequest.setrequestheader("content-type",                         "application/x-www-form-urlencoded");                 xmlhttprequest.send("stopname="+encodeuricomponent(stopname));             }              /*              * returns function waits state change in xmlhttprequest              */             function getreadystatehandler(xmlhttprequest) {                  // anonymous function returned                 // listens xmlhttprequest instance                 return function() {                     if (xmlhttprequest.readystate === 4) {                         if (xmlhttprequest.status === 200) {                             alert(xmlhttprequest.responsetext);                         } else {                             alert("http error " + xmlhttprequest.status + ": " + xmlhttprequest.statustext);                         }                     }                 };             } 

i want send stopname , and again send client using ajax please me using javascript not jquery.actually want send data , save database s way want test

i think might come because u have parameter in wrong way.

string stopname =  request.getparameter("stopname") != null ? request.getparameter("stopname").tostring() : "null value"; 

it handle null condition.

try code.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -