0

I'm trying to just get a simple response from but I have been banging my head against a wall for a couple hours now trying to figure this out.

Here's what I have for my doPost method:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {       
    processRequest(request, response);

    String message = "You reached the servlet and successfully got stuff back!";

    request.setAttribute("message", message);

    PrintWriter writer = response.getWriter();

    writer.println("Hello World");
}

I used both setAttribute and the writer from the response object. I've tried multiple variations from different Stack Overflow posts trying to figure this out, but I haven't had any luck. I figure I must be doing something wrong here, but I'm just totally clueless as to where I'm going wrong.

Here is what my AJAX call looks like:

window.onload=function(){
        var classHighlight = 'highlight';
        var $thumbs = $('.entry').click(function(e) {

        var strDate = $(this).text();

        var client;
        var data;
        var url_action="GetData";
        if(window.XMLHttpRequest)
        {
            client=new XMLHttpRequest();
        }
        else
        {                
            alert("ASDF");
        }
        client.onreadystatechange=function()
        {
            if (client.readyState==4 && client.status==200)
            {                     
                var data = client.responseType;
                console.log(client);
                alert(data);
            }
        };

        client.open("POST",url_action,true);
        client.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        client.send(strDate);
    });
    }

When I run my site, and I trigger the event that calls the AJAX event, I think it's working, but I'm not sure really how to tell. The onreadystate function triggers and does it's thing, but the response (in any form) is blank afterwards.

If anyone has any pointers, I'd really appreciate it. I'm completely lost on this and haven't had much experience (or even where to really look for help other than this darn blasted site).

Here is a link to my github with this site's source: https://github.com/Zulukas/engbud

Zulukas
  • 1,180
  • 1
  • 15
  • 35

1 Answers1

0

Are you sure your URL is correct ? Also i suggest debugging XHR with Chrome DevTools.

Wirst thing you need to do is enable XHR logging with right click on console window. enter image description here

After you should navigate to network tab where you can observe all your requests, sent data, and received answers.

enter image description here

More reading on this topics:

XHR on MDN

About Chrome DevTools

Sergei Panfilov
  • 1,191
  • 9
  • 15
  • I double checked the URL and yes, it's correct. Also, I cannot seem to follow your guide past the first step as there is no option to even "Log XMLHttpRequests" within my Chrome browser based on what you explained to me. I am able to see my XHR stuff under the network tab, but for the response it's completely blank, and the response header says the data is blank. I added a link to my git repo for this project in my original question. – Zulukas Mar 04 '16 at 16:13