0

I am trying to pass a string to a jsp file, and then I hope the jsp file will do something with the string. I don't know jsp though. I just want to make sure that my logic is correct. My code "works", but I want to make sure of what it does. I think I am posting the val of the string to browser's memory and calling the test.jsp file. Is that what is happening? Or should I do something completely different?

<!DOCTYPE html>

<html>
<head>
<title>Page Title</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
    $(document).ready(function(){
        var qString = '';
        $('#execute').click(function(){
        qString = $('#query-string').val();
        console.log(qString);
        $.post('test.jsp',qString);
        });
    });


</script>

</head>

<body>
<section>
    <label for="query-string">Enter your query here!</label>
    <input type="text" id="query-string">
        <button id="execute">Execute</button> 

</section>



</body>
</html> 

1 Answer 1

1

send your value as object and not string

 $(document).ready(function(){
    var qString = '';
    $('#execute').click(function(){
      qString = $('#query-string').val();
      console.log(qString);
      $.post('test.jsp',{qString:qString},function(data){
         //your stuff .. 
         alert('successfull');
      });
});

so this will post your values with a name qString to test.jsp page ..

{qString:qString} here key (qString) is the name by which the data is posted to jsp page (it can be anything) example {test:qString} , here data is posted as test so in jsp page you have to get the posted data by test...

function(data){....}); this is a callback function which is called when your post method is successfully called and returns some data from the server

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.