0

So, I'm having trouble retrieving information from my the client-side jsp. The javascript executes, and the alert prints, however query becomes null in the java servlet, and null is then written to the logger. I can't seem to figure out why the query is now null.

HTML:

<div id="query">
    <div id="querybar">
        <form onsubmit="query();return false;" method="get">
        <input type="text" id="querytext" placeholder="Run a query" name="querytext">
        </form>
        <div id="queryimg-container">
            <img src="styles/magnifyingglass.png" id="queryimg" alt="" />
        </div>
    </div>
</div>

JS:

function query() {
    $.get('QueryHelper', function(data) {
        alert("Somesortofalert");
    });
}

Java Servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String query = request.getParameter("querytext");
    response.setContentType("text/plain");
    @SuppressWarnings("resource")
    PrintWriter writer = response.getWriter();
    sLogger.info(query);
}

Can anyone see anything wrong? I'm super stumped here.

Jake
  • 205
  • 2
  • 11
  • Please describe what user action you take and then what you observe happening in both client and server. "trouble retrieving information" is not a complete description of what you observe. – jfriend00 Jan 26 '15 at 21:45
  • "I'm having trouble" is not a good description. Please explain what the behavior you experience is, and what is the behavior you expect. – RealSkeptic Jan 26 '15 at 21:46
  • What are you getting from your ajax call? try to add `.fail(function(){})` to your `$.get` call to see what is wrong... – albciff Jan 26 '15 at 21:46
  • @jfriend00 and RealSkeptic. Woops, I skipped that part and pasted my code. Forgot to go back to it. Fixed! – Jake Jan 26 '15 at 21:48
  • @albciff The response doesn't seem to fail, adding the .fail(function() {alert("failed)} portion does not seem to do anything. My problem is that getParameter("querytext"); seems to return null, as opposed to the intended return of whatever is in my textbox when I hit enter. – Jake Jan 26 '15 at 21:51
  • Your query() function is not submitting anything. It is calling the QueryHelper URL with no parameters and providing a callback. Have a look at the JQuery documentation on how to serialize and post a form. – Nick Vasic Jan 26 '15 at 22:03
  • @NickVasic Read through the documentation pertaining to **Jonathan Lonowskis** answer, and that totally makes sense. Sorry about that, I'm new to JS/JQuery. – Jake Jan 26 '15 at 22:07

1 Answers1

1

It'll be null because no parameter by that name is being sent with the Ajax request.

Despite being started by an onsubmit event, $.get() doesn't have any automatic knowledge of the <form>. It's up to you to gather or specify any parameters to be included with the request using the data parameter.

$.get('QueryHelper', { querytext: 'foo bar' }, function (res) {
    // ...
});

Provided you have a reference to the <form> or its inputs, you can use .serialize() to prepare the fields' names and values as data.

<form onsubmit="query(this); return false;" method="get">
<!--                  ^^^^                            -->
function query(form) {
    //         ^^^^

    $.get('QueryHelper', $(form).serialize(), function (res) {
        //               ^^^^^^^^^^^^^^^^^^^

        // ...
    });
}
Jonathan Lonowski
  • 112,514
  • 31
  • 189
  • 193
  • Awesome! I ended up just using the first part and replacing 'foo bar' with $('#querytext').val(). Does exactly what I want it to do! Are there any inherent advantages to the second option? Or are they simply two ways of doing it? Also, isn't this more of a post than a get, as I'm posting things to the servlet? Therefore, shouldn't I change everything to post? – Jake Jan 26 '15 at 22:00
  • 1
    @Jake In this case, retrieving the single, text field directly is probably simpler. `.serialize()` helps especially with larger forms using multiple input types. – Jonathan Lonowski Jan 26 '15 at 22:19
  • I assumed that was most likely the difference. Since the serialization solution seems to work, I think I'll just leave it at that. Thanks again! – Jake Jan 26 '15 at 22:31
  • @Jake For `GET` vs. `POST`, there are already some posts on SO that can help with choosing: http://stackoverflow.com/questions/3477333/ and http://stackoverflow.com/questions/504947/. – Jonathan Lonowski Jan 26 '15 at 22:33