Click here to Skip to main content
15,668,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

i am very new to javascript. how to call restful web services using javascript.

function CreateXMLHttpRequest() {
                   if (typeof XMLHttpRequest != "undefined") {
                       //All modern browsers (IE7+, Firefox, Chrome, Safari, and Opera) uses XMLHttpRequest object
                       return new XMLHttpRequest();
                   else if (typeof ActiveXObject != "undefined") {
                       //Internet Explorer (IE5 and IE6) uses an ActiveX Object
                       return new ActiveXObject("Microsoft.XMLHTTP");
                   else {
                       throw new Error("XMLHttpRequestnot supported");
               function CallService() {

                   var objXMLHttpRequest = CreateXMLHttpRequest();

                   objXMLHttpRequest.onreadystatechange = function () {
                       if (objXMLHttpRequest.readyState == 4)

         "POST", "http://localhost:2546/abc.svc/json/GetXml", true);
                   objXMLHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                   //  objXMLHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
                   var packet = '<?xml version="1.0"encoding="utf-8"?><CompanyRequest xmlns="" xmlns:i=""><Company>company1</Company></CompanyRequest>';


i need to send xml request and response should also be xml. i am getting null as response. correct if i am wrong..

thanks in advance...
Updated 20-Nov-12 0:49am

This is sample how to use ajax to get xml data source.

	type : "GET",
	url : "sites1.xml",//urlOftheXml 
	dataType : "xml",
	success : function(xml) {
	$(xml).find('func').each(function() {
	var number1 = $(this).find('number1').text();
	$(this).find('case').each(function() {
		if ($(this).find('reserved').text().length == 0) {
		var data = $(this).find('data').text();
		var bitoffs = $(this).find('bitoffs').text();
		var temp = parseInt(str.substr(bitoffs, bitsize), 2);
		alert("number :" + number1 + "data :" + data + temp);
Share this answer
If you are consuming the REST from a different site, then you need to used either JSONP or easyXDM to consume it with Javascript.[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900