Invoking REST webservice from JQuery using an AJAX call

Below code helps you to understand how we can Invoke a REST web service on click of a button using JQuery and AJAX.

Note: In Java you can Invoke REST web service using ApacheHTTPClient library.

HTML File:

<!DOCTYPE html>
<html>
<head>
<sscript type=”text/javascript”
src=”http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js”></script&gt;
<sscript type=”text/javascript”>
$(document).ready(function () {
$(“#mybutton”).click(function () {
alert(‘welcome’);

$.ajax( {
type : “GET”, url : “http://services.groupkt.com/country/get/iso2code/IN&#8221;, dataType : “json”, success : function (data) {
alert(“Reading Msg: ” + data.RestResponse.messages);
alert(“Reading Name: ” + data.RestResponse.result.name);
}
});

});

});
</script>
</head>
<body>
<button type=”button” id=”mybutton”>Invoke REST WS</button>
</body>
</html>

Sample REST Services (thanks for making them available to developers for testing purpose):

http://www.groupkt.com/post/c9b0ccb9/country-and-other-related-rest-webservices.htm
http://services.groupkt.com/country/get/iso2code/IN
http://services.groupkt.com/country/get/all
http://rest-service.guides.spring.io/greeting

REST_invoke
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s