PDA

View Full Version : getting json without form



andynuss
21 Sep 2009, 6:23 AM
Hi,

What is the best way to set a json response from the server without using a form at all. I.e. there is no user data to fill out. Just get the value of a cookie and make a request to the server with that cookie, and the response should be json data.

Andy

Mike Robinson
21 Sep 2009, 6:26 AM
That's called an Ajax.Request.

Think of an Ajax request as "a remote procedure-call." That's really what it is:


A packet of information, e.g. in JSON format, is sent to some URL on the host. Perhaps the URL itself identifies the request ("RESTful"), or maybe a required parameter in the packet itself. (Think of that packet of information as a 'structure' or 'record.' It consists of a list of named parameters, each with values that might be values, lists, or other structures. JSON is simply the agreed-upon, lightweight, encoding-format that is used.)
The server does something with the packet, e.g. calls a subroutine which formulates some response.
The response, including at least "success = true," is encoded in JSON format by the host and sent back to you.

That's it... that's all. "What does the request packet contain?" Anything that you want it to, and nothing else. If you want to include Form data with your request, you easily can. But if you don't, you don't have to.

Since the HTTP protocol is being used, the packets are accompanied by cookies, etc.

When you requested a page the first time, you made an HTTP request for it, and the browser displayed whatever (HTML...) the server returned. But JavaScript can make its own HTTP requests too, without you "seeing it," and can do whatever it wishes with what the host sends back. The exchange doesn't have to contain "HTML," and it probably doesn't. All of this is called ... Ajax.

By far the easiest way to "grok" this is to get the Firebug plug-in for Firefox and learn how to use it. You can see what the host and client are saying to each other... headers, content and all. Worth a thousand words.

andynuss
21 Sep 2009, 8:00 AM
Hi,

I tried this and haven't gotten it to work yet.

My java servlet test code:


response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-json");
PrintWriter writer = response.getWriter();
writer.write("{success:true, data:");
writer.write("{numpages:[{count:0},{count:1},{count:2}]}");
writer.write("}");
writer.flush();


And my extjs code:


Ext.Ajax.request({
url : 'bookservlet/0',
success : function (form, action) {
var data = action.result.data;
Ext.Msg.show({
minWidth : 300,
buttons : Ext.Msg.OK,
icon : Ext.Msg.ERROR,
msg : "" + data.numpages[2].count,
});
},
failure : function (form, action) {
Ext.Msg.show({
minWidth : 300,
buttons : Ext.Msg.OK,
icon : Ext.Msg.ERROR,
msg : action.failureType,
});
},
});


Any ideas why firefox says action.result is undefined? For what my servlet sent, shouldn't I be accessing action.result.data?
Andy

mrsunshine
21 Sep 2009, 1:14 PM
how does the response look like if you open the firebug net panel and open the response
or
just open the service url in the browser and show the result.

Is it a valid json object?