[jQuery] AJAX and JSON
[jQuery] AJAX and JSON
I have some code working that invokes an HTTP service that returns JSON.
From my JavaScript I call the service with:
$.getJSON(url + "?callback?", function (json) {
// do something with json here
});
My service is implemented as a Java servlet with a doGet method.
In that method I:
1) set the content type of the response to "application/x-javascript".
2) get the value of the callback parameter
3) create a Java Map
4) use the org.json library to convert the map to JSON text
5) build a string of JavaScript code that calls the callback function,
passing it the JSON
6) write the string of JavaScript to the response
Is this the recommended approach? I was expected that
I wouldn't need to pass the callback function name to the servlet and
could just return the JSON from the servlet instead of a string of
JavaScript code.
--
R. Mark Volkmann
Object Computing, Inc.