By S.Luann
Request a resource
function getLastShipmentLocation( String shipmentID ){
var URLString = "https://localhost:8080/GlobalTracking/shipments/" + shipmentID;
//Issue AJAX request to Shipment RESTful service with last location as parameter
new Ajax.Request(URLString, {
method: 'get',
parameters: {location: 'last', limit: 12}
});
}
You can see that the concept of resources in REST is represented as Urls
An Ajax library used in above code wraps asynchronous invocations of XMLHttpRequest and cross-browser compatility issues.
Consume Service Result
A service can response the result with plain text, XML or JSON.
List 1: consume plain text result
// parse text delimited with ","
var values = httpRequester.responseText.split(",");
document.getElementById('messageBody').value = values[3];
List 2: consume XML result
<shipment>
<tracking_no>100200345100</tracking_no>
<status>in transit</status>
<location>LA</location>
<date>12-12-2007</date>
</shipment>
var xmlInfo = httpRequester.responseXML;
//get tracking_no using DOM. It is a single element, so go for text value of the first node
var trackingNo = xmlInfo.getElementByTagName("tracking_no")[0].firstChild.nodeValue;
List 3: Consume JSON result
{"shipment":{
"tracking_no":"100200345100",
"status":"transit",
"location":"LA",
"date":"12-12-2007"
}
}
var shipmentData = eval('('+httpRequester.responseText+')');
//access tracking number by dereferencing the value using the 'dot' notation
var tracking_no = shipmentData.shipment.tracking_no;
Reference
1. Edmon Begoli, Integrating AJAX Clients and RESTful Web Services
http://www.devx.com/enterprise/Article/35013
No comments:
Post a Comment