0

I want to call rest services using javascript. My code is:

function CreateXMLHttpRequest() {
   if (typeof XMLHttpRequest != "undefined") {
      alert("1");
      return new XMLHttpRequest();
   }
   else if (typeof ActiveXObject != "undefined") {
      alert("2");
      return new ActiveXObject("Microsoft.XMLHTTP");
   }
   else {
      throw new Error("XMLHttpRequestnot supported");
   }
}

function CallWebService() {
   var objXMLHttpRequest = null;
   objXMLHttpRequest = CreateXMLHttpRequest();

   objXMLHttpRequest.open("POST", "http://localhost:2546/abc.svc/json/GetXml", true);
   objXMLHttpRequest.setRequestHeader("Content-Type", "application/xml;charset=UTF-16");
   var packet = '<CompanyRequest xmlns="http://schemas.datacontract.org/2004/07/abc.DomainModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><CompanyName>company</CompanyName></CompanyRequest>';

   objXMLHttpRequest.send(packet);
      alert(packet);
      var d =(objXMLHttpRequest.responseText);
      alert(d);           
   }

It is working fine in IE, but in Firefox and chrome the response is empty. I can't understand what is going wrong. I have been searching for this for 3 days. Let me know if there are any errors.

Thanks in advance....

vidhya
  • 1
  • 4

2 Answers2

1

You're making async call. Try to use callback.

function CreateXMLHttpRequest() {
     if (typeof XMLHttpRequest != "undefined") {
         return new XMLHttpRequest();
     } else if (typeof ActiveXObject != "undefined") {
         return new ActiveXObject("Microsoft.XMLHTTP");
     } else {
         throw new Error("XMLHttpRequestnot supported");
     }
 }
 function CallWebService() {
     var objXMLHttpRequest = CreateXMLHttpRequest();
     objXMLHttpRequest.open("POST", "http://localhost:2546/abc.svc/json/GetXml", true);
     objXMLHttpRequest.setRequestHeader("Content-Type", "text/xml");
     var packet = '<CompanyRequest xmlns="http://schemas.datacontract.org/2004/07/abc.DomainModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><CompanyName>company</CompanyName></CompanyRequest>';
     objXMLHttpRequest.onreadystatechange = function (){
         if (objXMLHttpRequest.readyState==4 && objXMLHttpRequest.status==200) {
             alert(objXMLHttpRequest.responseText);
         }
     }
     objXMLHttpRequest.send(packet);
 }
devsathish
  • 2,339
  • 2
  • 20
  • 16
0

You are doing an async request.

Try doing a synchronous request like

 objXMLHttpRequest.open("POST", "http://localhost:2546/abc.svc/json/GetXml", false);

Or asynchronusely

objXMLHttpRequest.onreadystatechange = function () {
    if ( objXMLHttpRequest.readyState == 4 ) {
        alert(req.responseText);
    }
};

And set the header to:

Try this:

 objXMLHttpRequest.setRequestHeader("Content-Type", "text/xml");

 objXMLHttpRequest.setRequestHeader( "SOAPAction", "http://localhost:2546/abc.svc/json/GetXml" );
Akhil Sekharan
  • 12,467
  • 7
  • 40
  • 57
  • This will work. But it'll freeze the UI from doing any other actions until the response comes. – devsathish Nov 21 '12 at 08:03
  • @Vidhya: Sorry. Just wrote a raw code. I always use variable name 'req' for XMLHttpRequest. Updated. – Akhil Sekharan Nov 21 '12 at 08:15
  • no error it is displaying?just empty alert box...it is working in IE but not in chrome and firefox – vidhya Nov 21 '12 at 09:06
  • Did you try to do it synchronously? Use my first option to test. If the alert box is still empty, then you might want to check your server side code. – Akhil Sekharan Nov 21 '12 at 09:18
  • Try this: objXMLHttpRequest.setRequestHeader("Content-Type", "text/xml"); objXMLHttpRequest.setRequestHeader( "SOAPAction", "http://localhost:2546/abc.svc/json/GetXml" ); – Akhil Sekharan Nov 21 '12 at 09:31