Click here to Skip to main content
13,452,284 members (52,826 online)
Rate this:
Please Sign up or sign in to vote.
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...
Posted 16-Nov-12 22:16pm
Updated 20-Nov-12 0:49am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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);
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

If you are consuming the REST from a different site, then you need to used either JSONP or easyXDM to consume it with Javascript.[^]

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.180318.3 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100