HTML and CSS Reference
In-Depth Information
ToJSDate() uses regular expression and the JavaScript exec() method. exec() is called on a regular
expression and accepts a parameter. It tests the specified value against the regular expression and returns
the matched text. A new JavaScript Date object is then constructed, and a date string in mm/dd/yyyy
format is returned to the caller.
n Note JSON doesn't have a specific format to represent date and time. This poses difficulty when you're
serializing and deserializing date-time data between client and server. A recent trend is to use the ISO date and time
format on the wire (for example, Json.NET and the Web API use this format). In ISO format, a date and time are
represented as follows: YYYY-MM-DDThh:mm:ssTZD (for example, 2010-08-20T19:20:10+01:00).
After the order details are displayed in the HTML table, the button is enabled again by removing the
disabled attribute added previously. The button value is again set to Get Orders.
The event handler of the error event simply displays an error message in an alert box, as follows:
function HandleError(evt) {
var msg="There was an error in the worker thread!\r\n\r\n";
msg += "Message : " + evt.message + "\r\n";
msg += "Source : " + evt.filename + "\r\n";
msg += "Line No. : " + evt.lineno;
alert(msg);
}
The HandleError() function displays the error message, source file name, and line at which the error
occurred using the three properties of the ErrorEvent object: message , filename, , and lineno .
Code Running in the Web Worker
Now that you've finished coding the view, let's move to the Processing.js file that is executed by the web
worker. The JavaScript from Processing.js retrieves order data from the server using the XMLHttpRequest
object. Listing 10-12 shows the contents of Processing.js .
Listing 10-12. Retrieving Data from the Server Using XMLHttpRequest
addEventListener("message", ReceiveMessageFromPage, false);
function ReceiveMessageFromPage(evt) {
GetOrders(evt.data);
}
function GetOrders(settings) {
var xhr = new XMLHttpRequest();
xhr.open("POST", "/Home/GetOrders");
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
var data = JSON.parse(xhr.responseText);
var orderAmount = 0;
var finalData = new Array();
var currOrderId = data[0].OrderID;
 
 
Search WWH ::




Custom Search