Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript XML REST
Hi...
 
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)
                          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 16-Nov-12 22:16pm
074051.1K
Edited 20-Nov-12 0:49am
v9
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This is sample how to use ajax to get xml data source.
 
$.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);
		}
	   });
	});
    }
});
  Permalink  
Rate this: bad
good
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.
 
http://easyxdm.net/wp/
 
http://easyxdm.net/wp/2010/03/17/cross-domain-ajax/[^]
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 554
1 Gihan Liyanage 338
2 RyanDev 268
3 Sergey Alexandrovich Kryukov 263
4 ChauhanAjay 220
0 Sergey Alexandrovich Kryukov 8,936
1 OriginalGriff 7,891
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2014
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