reset password
Author Message
plakhan
Posts: 37
Posted 21:31 Feb 11, 2015 |

Hello Professor,

For ajax, we need to use the http request, response to read the data and pass the data back to the client.

Am i right ?

 

cysun
Posts: 2935
Posted 08:37 Feb 12, 2015 |

Not quite. From the server's perspective, an Ajax request (i.e. XmlHTTPRequest) is just like any other requests, which means you can use Spring controllers and views to handle it just like any other requests.

With that said, the views returned to the client are usually different: for regular requests, the view is usually a complete HTML or JSP page; for Ajax requests, the "view" is usually an empty HTTP response, a partial page, or some data.

1. Empty HTTP Response

For some Ajax operations (like checking/unchecking a checkpoint in HW5), the client only needs to know whether the operation is successful or not, so the server can send back a response with an empty body and use the response status code to indicate whether the operation is successful. To generate these types of responses you can either use HttpServletResponse directly, or use a slightly more convenient way provided by Spring (i.e. @ResponseBody and @ResponseStatus) - see addCourse() and removeCourse() in CourseMappingControllerS in CSNS2 for examples.

2. Partial Page

This is just like regular views except the view (e.g. a JSP) is only part of a page.

3. Data

Sometimes the server needs to send back some data, and the JavaScript code on the client side will use the data to update the HTML document. The data is usually in JSON format. We'll discuss this later when we talk about web services.