HTML and CSS Reference
In-Depth Information
This code shows a JavaScript function— GetOrders() —intended to retrieve order data for a specific
customer from the SQL Server. The GetOrders() function accepts a customer ID whose orders are to be
retrieved.
Inside, the function creates a new instance of XMLHttpRequest . The open() method of the
XMLHttpRequest object opens a communication channel with a remote request. The first parameter of the
open() method is the type of request (GET/POST) being made, and the second parameter is the location of
the remote resource. In this example, a POST request is made to an MVC action method named GetOrders .
The XMLHttpRequest object's setRequestHeader() method sets the MIME content type of the request to
application/json .
The XMLHttpRequest object raises the onreadystatechange event when the state of the request changes.
The onreadystatechange event-handler function checks the readyState property of the XMLHttpRequest
object. The readyState object indicates the current state of the request and can have values from 0 to 4 . A
value of 4 indicates that the request is completed. If the request is completed, you can use the
postMessage() method to send the data returned by the remote resource back to the web page. The
responseText property of the XMLHttpRequest object returns the response from the remote resource. In this
case, the MVC action method is assumed to return data in JSON format, and hence responseText is sent
directly to the web page.
To initiate the request, you need to call the XMLHttpRequest object's send() method. send() takes
request parameters, if any. Because you wish to send request data in JSON format, you convert the
customer ID to a JSON object and then pass it as a parameter to send() .
Using Web Workers That Require Server-side Data
In this final example, you develop an ASP.NET MVC application that uses web workers to fetch data from
the server and display it in a view. The main view of the application is shown in Figure 10-8.
Figure 10-8. Order History application
 
Search WWH ::




Custom Search