HTML and CSS Reference
In-Depth Information
Figure 10-7. Observing shared worker threads using the Resource Monitor
Communicating With the Server
Web workers may need to make requests to server-side resources for their functioning. For example, a
script running in a web worker may need to retrieve data residing in a SQL Server database. To fulfill this
requirement, you may immediately think of using the jQuery $.ajax() method. After all, throughout this
book you've been using jQuery to make Ajax calls to the server. However, you can't use jQuery with web
workers because jQuery requires DOM access. As mentioned previously, web workers don't have access to
any DOM elements. So, if you wish to make Ajax calls to the server, you need to use the XMLHttpRequest
object.
Listing 10-7 uses XMLHttpRequest to access a server-side resource.
Listing 10-7. Using the XMLHttpRequest Object to Make Ajax Calls
function GetOrders(customerId) {
var xhr = new XMLHttpRequest();
xhr.open("POST", "/Home/GetOrders");
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
postMessage(xhr.responseText);
}
}
var param = '{ "customerid": "' + customerId + '"}';
xhr.send(param);
}
Search WWH ::




Custom Search