I am writing a web service interested in the following script.

  1. There is an html file with a table and a button.
  2. When you select a field in the table, the field is assigned a new class "active"
  3. I click on the button
  4. I wish in the servlet to get the data that is in the first column of the selected field.

The question is how to organize a post-request to the servlet via js and send the data?

Now, if we take data from the form, then everything works out correctly.

And I would like not to resort to using the form, but let the script run around the required data and send it all over the entire Html file.

Script file:

function getXmlHttp() { var xmlHttp = null; if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } return xmlHttp; } function httpReq(URL, method, data, success, error) { var request = getXmlHttp(); request.open(method, URL, true); request.setRequestHeader("Content-type", "multipart/form-data"); request.send(data); request.onreadystatechange = function() { if (request.readyState == 4) { if (request.status == 200) { success(request.responseText); } else { if (error) error(request.status); } } } } var table = document.querySelector("#selectTable") var field = table.querySelector(".active"); var item = field.children[0]; // первый дочерний var form = new FormData(); form.append("some_key", item.innerHTML); // ну или что там нужно вытащить httpReq("/admin", "POST", form, function(res) { console.log("response:", res); }); 
  • I advise you to see the documentation for getElementById and querySelector - or better yet, use some library - Aleks G

1 answer 1

 function getXmlHttp() { var xmlHttp = null; if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } return xmlHttp; } function httpReq(URL, method, data, success, error) { var request = getXmlHttp(); request.open(method, URL, true); request.setRequestHeader("Content-type", "multipart/form-data"); request.send(data); request.onreadystatechange = function() { if (request.readyState == 4) { if (request.status == 200) { success(request.responseText); } else { if (error) error(request.status); } } } } someBtn.onclick = function(e) { var table = document.querySelector("селектор таблицы") var field = table.querySelector(".active"); var item = field.children[0]; // первый дочерний var form = new FormData(); form.append("some_key", item.innerHTML); // ну или что там нужно вытащить form.append("another_key", another_value); httpReq("url...", "POST", form, function(res) { console.log("response:", res); }, function(err) { console.error(err); }) }; 
  • readyState swears at readyState and status ( - Vorobey.A
  • "something swears" loose concept. I'm not an extrasensory. - Constantine
  • admin: 125 Uncaught TypeError: Cannot read property 'readyState' of null - Vorobey.A
  • Is the httpReq function unchanged? Show the script file so that the context can be seen. - Constantine
  • updated the description - Vorobey.A