0

I have a simple test client-server app. Client is html/javascript, server - Java Servlet

First of all I want to test request/response mechanism. Therefore I have used a simple code for cliet(jQuery):

$.get ("http://localhost:8081/TestProject/BasicServlet",
       function(data) {
          alert('Data:' +data);
       }
);

And on the server side:

protected void doGet(HttpServletRequest req, HttpServletResponse res) ... {
      String callBack = "TestCallback";
      res.setContentType("text/html");
      ServletOutputStream out = res.getOutputStream();
      out.write(callBack.getBytes("UTF-8"));
      out.flush();
}

So, Servlet catches request from client, but I have a problem with response, response header looks good, with character attributes, but I don't receive the callBack data

As response in Firebug I have 3 tabs, Header, Answer, HTML. Answer and HTML are empty

EDIT: I have found a Problem: it was Access-Control-Allow-Origin violation. Thanks for help !

6
  • 2
    Your JavaScript looks invalid (is that a function call? Where are the ()? How do you expect an object with no values for any of its keys to work?), and I've no idea what $ is. Prototype.js? Mootools? Commented May 3, 2011 at 15:50
  • Check to see in firebug if "TestCallback" is in the response. Commented May 3, 2011 at 15:50
  • Ah, so he did. The rest of my comment still applies though. Commented May 3, 2011 at 15:56
  • That's was a typo in my post, because I post from another computer and my code is on the other one Commented May 3, 2011 at 16:00
  • 1
    Start learning how to use debuggers. Commented May 3, 2011 at 16:11

2 Answers 2

1

As per the documentation in here

http://download.oracle.com/javaee/6/api/javax/servlet/ServletResponse.html#getOutputStream

is used for sending binary data. So my guess is that Content-Type header is set as some MIME type which is not recognized by jQuery. I suggest you check whether the Content-Type header is still "text/html" in the response using FireBug, or use

PrintWriter writer = res.getWriter();
writer.write(callBack);
writer.flush();

By the way, for sending textual data using PrintWriter is the recommended approach.

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

1 Comment

I don't think the text/plain is a requirement.
0

Try out.print() instead of out .write() you will get the response in your ajax call.

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.