Sending value from javascript to jsp (with jquery)

right now I have a jsp page that allows you to sort some elements when they are ready, and clicked the link, the javascript function converts all the information into xml (text in a variable), after that I need to send this xml to on the jsp page again, I tried put information in hidden input and submit the form, sending with $ .post and a few more jquery functions, but nothing worked. Any ideas?

in my JSP I read a message like this:

<% out.println(request.getParameter("data")); %>

This does not work:

xml = "<xml></xml>";
$("#form").submit(function(){
   alert("JS: " + $("#data").text());
   $("#data").text(xml);
});

It's the same:

xml = "<xml></xml>";
$("#data").text(xml);
$("#form").submit();

replacing .text with .html does not work

Any ideas are welcome, thanks

+5
source share
3 answers

, . html-, , :

<form method="POST" id="form">
    <input type="hidden" id="data" />
</form>

, $("#data").val(xml); text() html(), html, . .

jQuery $.post() xml, , . javascript- , , jQuery $.post(), $.get() .. :

var values = {name: "John", surname: "Doe"};
values.age = 25;
$.post("index.jsp", values); // this will result in a post with 3 variables: name, surname, age

, xml ( -):

$.post("index.jsp", {data: "<xml><whatever-else-needs-to-be-in-here/></xml>"});

, : Ajax @jQuery docs

+3

jQuery Ajax API - GET POST, - .

+2

You can always use XMLHttpRequest to send data. This can be done without our interaction with the user, for example, with the submit form button. jQuery has built-in functions to support such queries.

http://docs.jquery.com/Ajax

+1
source

All Articles