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

i want to call rest services using javascript. my code is

C#
function CreateXMLHttpRequest() {


             if (typeof XMLHttpRequest != "undefined") {
                 alert("1");
                 //All modern browsers (IE7+, Firefox, Chrome, Safari, and Opera) uses XMLHttpRequest object
                 return new XMLHttpRequest();
             }
             else if (typeof ActiveXObject != "undefined") {
                 alert("2");
                 //Internet Explorer (IE5 and IE6) uses an ActiveX Object
                 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 = '<GetCompanyRequest xmlns="http://schemas.datacontract.org/2004/07/abc.DomainModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><CompanyName>company</CompanyName></GetCompanyRequest>';
//          
             objXMLHttpRequest.send(packet);
            
            alert(objXMLHttpRequest.responseText);                  

         }


Its working fine in IE and status is 200. But in chrome and firefox status is 0 and response is empty. And also how can i parse the response.....
let me know if any errors.
thanks in advance....
Posted
Updated 22-Nov-12 20:06pm
v4

1 solution

Try this way to check the Browser Compability,
C#
if(window.XMLHttpRequest)
            {
                // Other Browser
                ajaxRequest = new XMLHttpRequest();
            }
            else
            {
                // Internet Explorer
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            ajaxRequest.onreadystatechange=function()
                {
                    if(ajaxRequest.readyState == 4)
                        {
                            document.getElementById("YourOutPutID").innerHTML = ajaxRequest.responseText;
                        }
                }
                ajaxRequest.open("GET", "index.php?vote="+vote,true);
                ajaxRequest.send();

          }
 
Share this answer
 
Comments
07405 21-Nov-12 2:28am    
i need it for post method.can u help me

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