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

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

C#
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)
                          objXMLHttpRequest.responseText;

                   }
                   objXMLHttpRequest.open("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="http://schemas.datacontract.org/2004/07/abc.DomainModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Company>company1</Company></CompanyRequest>';

                   objXMLHttpRequest.send(packet);
               }


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...
Posted
Updated 20-Nov-12 0:49am
v9

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

JavaScript
$.ajax({
	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.

http://easyxdm.net/wp/

http://easyxdm.net/wp/2010/03/17/cross-domain-ajax/[^]
 
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