0

Hello everyone,
I am really struggling with ajax and was wondering if someone could help me send a string as data by jquery's post method to my servlet.

My post does not work, and when I try to receive it from my servlet, the whole thing blows up :(

In an if statement, when the condition is true I have

$(document).ready(function() {
        $.post("Diagnostic",dataString
    });

Can someone guide me through this, please

2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by scrappedcola
0

What error are you getting? How are you calling the post? You say it's inside an if block but you also have it wrapped inside a ready function which isn't really necessary at that point since if the if block is good the document is loaded already. Generally you only need to wrap your code in a ready block when you need to interact with the dom or use data from the client side that requires the entire doc to be loaded. Also are you using

public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
}

in the servlet code? A good basic example I saw for integrating Ajax with servlets is here: http://veerasundar.com/blog/2008/12/implementing-ajax-in-java-web-application-using-jquery/ More complete code examples of what you are doing and the error would help in further debugging. if you aren't using either Fiddler, Tamper Data, or Firebug you really need to get one of them as they really help in debugging ajax since you are able to see the request/data being sent to the server and also see the server's response.

0

I actually started my work based on that example. So what can I use in the if statement, jus the post ?

I'll post some code soon

0

Yes. For example if you had a click event defined on the actual anchor element you could do this:

<a href='javascript:void(0)' onclick='postToServlet("postMe")'>This link has a click event</a>
<script>
function postToServlet(isPost)
{
   if(isPost)
   {
      $.post("Diagnostic",dataString});
   }
}
</script>

It's just guess work on what you are doing but should convey the idea ok. You could also attach the click event in another way and in that case you might want to do so in this manner:

<a href='javascript:void(0)' id='postLink'>This link has a click event</a>
<script>
$.ready(function()
{
   $("#postLink").postToServlet("postMe");
}
function postToServlet(isPost)
{
   if(isPost)
   {
      $.post("Diagnostic",dataString});
   }
}
</script>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.