background preloader

REST

Facebook Twitter

Populate a select using Ajax. In this post I’ll explain how to populate a select dropdownlist using jQuery and Ajax. I am using an ASP.NET web application and page methods to perform the Ajax calls. Using page methods means that you do not need a seperate web service, which is good if the functionality is specifically for the page. The page methods must be declared public static and use the WebMethod attribute. Download source First of all here is the contents of my form with two select lists, one for gender and one for names. Each list is given a unique ID to be able to reference them in jQuery. Gender Name Here is the page method that gets my list of genders: [WebMethod] public static ArrayList GetGenders() return new ArrayList() new { Value = 1, Display = "Male" }, new { Value = 2, Display = "Female" } For the purpose of the example I am just returning an ArrayList of an anonymous type containing a value and display text which will be converted to JSON in jQuery and used to populate the select list.

Else $.ajax({ else { Ajax. Description: Perform an asynchronous HTTP (Ajax) request. The $.ajax() function underlies all Ajax requests sent by jQuery. It is often unnecessary to directly call this function, as several higher-level alternatives like $.get() and .load() are available and are easier to use. If less common options are required, though, $.ajax() can be used more flexibly. At its simplest, the $.ajax() function can be called with no arguments: Note: Default settings can be set globally by using the $.ajaxSetup() function. This example, using no options, loads the contents of the current page, but does nothing with the result.

The jqXHR Object The jQuery XMLHttpRequest (jqXHR) object returned by $.ajax() as of jQuery 1.5 is a superset of the browser's native XMLHttpRequest object. As of jQuery 1.5.1, the jqXHR object also contains the overrideMimeType() method (it was available in jQuery 1.4.x, as well, but was temporarily removed in jQuery 1.5). Callback Function Queues Data Types Sending Data to the Server. GetJSON. Description: Load JSON-encoded data from the server using a GET HTTP request. This is a shorthand Ajax function, which is equivalent to: Data that is sent to the server is appended to the URL as a query string. If the value of the data parameter is a plain object, it is converted to a string and url-encoded before it is appended to the URL. Most implementations will specify a success handler: This example, of course, relies on the structure of the JSON file: Using this structure, the example loops through the requested data, builds an unordered list, and appends it to the body.

The success callback is passed the returned data, which is typically a JavaScript object or array as defined by the JSON structure and parsed using the $.parseJSON() method. As of jQuery 1.5, the success callback function receives a "jqXHR" object (in jQuery 1.4, it received the XMLHttpRequest object). Important: As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently. Post. Description: Load data from the server using a HTTP POST request. This is a shorthand Ajax function, which is equivalent to: The success callback function is passed the returned data, which will be an XML root element or a text string depending on the MIME type of the response. It is also passed the text status of the response. As of jQuery 1.5, the success callback function is also passed a "jqXHR" object (in jQuery 1.4, it was passed the XMLHttpRequest object).

Most implementations will specify a success handler: This example fetches the requested HTML snippet and inserts it on the page. Pages fetched with POST are never cached, so the cache and ifModified options in jQuery.ajaxSetup() have no effect on these requests. The jqXHR Object As of jQuery 1.5, all of jQuery's Ajax methods return a superset of the XMLHTTPRequest object.

Deprecation Notice The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callback methods introduced in jQuery 1.5 are deprecated as of jQuery 1.8. RESTEasy Reference. RESTful web services + Spring 3 MVC HttpMessageConverter feature. Introduction A companion article, "Build RESTful web services using Spring 3," (see Resources) introduced the "Spring way" to build RESTful web services. It also explained how to use ContentNegotiatingViewResolver to produce multiple representations, which is an important feature for RESTful web services. This article explains another way to produce multiple representations using HttpMessageConverter, and examples in the article show how to use RestTemplate with HttpMessageConverter to communicate with services. Back to top REST support in Spring MVC This section provides an overview of the major Spring features, or annotations, that support RESTful web services. @Controller Use the @Controller annotation to annotate the class that will be the controller in MVC and handle the HTTP request.

@RequestMapping Use the @RequestMapping annotation to annotate the function that should handle certain HTTP methods, URIs, or HTTP headers. For example: @PathVariable Other useful annotations Table 1. ATOM feed.