1

I got tomcat running on port 8080 and simple servlet:

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.*;

public class MyHelloWorld extends HttpServlet {

    public void doGet(HttpServletRequest request, 
            HttpServletResponse response)
    throws IOException, ServletException
    {
        String data = "Hello World from servlet!";
        response.setContentType("text/plain");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(data);
    }
}

wget on URL to this servelet, retrives a string:

"Hello World from servlet!"

also web browser prints it, so it works, and tomcat's access log shows response '200'

But when im trying to get this string via my javascript:

<html>

<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>


            $.getJSON({ 
    type: "GET", url: "http://localhost:8080/examples/MyHelloWorld", 
    contentType: "text/plain", 
    error: function(xhr, ajaxOptions, thrownError){ 
                alert(ajaxOptions);
                alert(xhr.status); 
                alert(thrownError);
            }, 
    processData: true, 
    success: function(data, textStatus, jqXHR){ alert(data); }
});
</script>

</script>

</head>

In tomcat's log i can see response 200, but browser does not show anything - just blank page with no content. If I change getJSON into get or ajax i get alerts with:

xhr.status = 0
ajaxOptions = "error"
thrownError = empty

web server is apache and it runs on port 80

Thx for all help

JosiP
  • 2,619
  • 5
  • 29
  • 33

4 Answers4

2

Try something like this:

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String data = "Hello World from servlet!";
    response.setContentType("text/plain");
    response.setCharacterEncoding("UTF-8");

    if (request.getContentType().equals("application/json")) {
        data = "\"" + data + "\"";
    }

    response.getWriter().write(data);
}

And in your js:

$.getJSON("http://localhost:8080/examples/MyHelloWorld", function(data) {
   alert(data)
})
Sergii Stotskyi
  • 5,134
  • 1
  • 22
  • 21
0

The output:

"Hello World from servlet!"

That is a string, not JSON object, run it through jsonlint.com. Your output should look something like:

{ "data" : "Hello World from servlet!" }

with the proper content type "application/json" set.

If you want to just get a plain text string, use $.get().

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • 1
    @Serjio HUH? What are you talking about? `JSON.parse("asdf")` will produce: _SyntaxError: JSON.parse: unexpected character_ – epascarello Aug 06 '12 at 14:48
  • `JSON.stringify(123) => "123"`, `JSON.parse("123") => 123`, `JSON.parse("\"123\"") => "123"` – Sergii Stotskyi Aug 06 '12 at 14:51
  • @Serjio guessing you are using Chrome, Run your code through http://jsonlint.com/ and see what that says. – epascarello Aug 06 '12 at 14:54
  • my code also works in FF. Also it works in PHP: `php > echo json_encode("123"); # ouput is "123"`. Just read JSON specification. Also about JSON on stackoverflow: http://stackoverflow.com/questions/383692/what-is-json-and-why-would-i-use-it – Sergii Stotskyi Aug 06 '12 at 15:01
  • It would have to be `JSON.parse('"foo"');` for parse to work in Firefox and IE. – epascarello Aug 06 '12 at 15:09
0

Write is like this,

 String data = "{\"message\":\"Hello World from servlet!\"}";

And on JS do,

alert(data.message);
Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47
0

Also, my java scripit console under chrome is saying:

GET http://10.10.1.19/[object%20Object] 404 (Not Found) jquery-latest.min.js:4
f.support.ajax.f.ajaxTransport.send jquery-latest.min.js:4
f.extend.ajax jquery-latest.min.js:4
f.each.f.(anonymous function) jquery-latest.min.js:4
f.extend.getJSON jquery-latest.min.js:4
(anonymous function)
JosiP
  • 2,619
  • 5
  • 29
  • 33