Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Plz anybody look at the following code and reply with code correction.

C#
<script type="text/javascript">
        function CallWcfAjax() {
            var xmlHttp = null;
            var val = navigator.userAgent.toLowerCase();
            if (val.indexOf("msie") > -1) {
            xmlHttp = new ActiveXObject("Microsoft.XmlHttp");
            }
            else {
            xmlHttp = new XMLHttpRequest();
            }

            var url = "http://localhost:1912/Service1.svc/ajaxEndpoint/";
            url = url + "Sum2Integers";
            var body = '{"n1":';
            body = body + document.getElementById("num1").value + ',"n2":';
            body = body + document.getElementById("num2").value + '}';

            //Send HTTP request
            xmlHttp.open("POST", url, true);
            xmlHttp.setRequestHeader("Content-type", "application/json");
            xmlHttp.send(body);

            //create result handler
            xmlHttp.onreadystatechange = X;
            function X() {
                if (xmlHttp.readyState == 4) {
                    var obj = null;
                    if (val.indexOf("msie") > -1) {
                        obj = xmlHttp.responseText;
                    }
                    else {
                        obj = xmlHttp.responseXML;
                    }


                    var newobj = eval("(function(){return " + obj + ";})()");
                    alert(newobj.d);
                    result.innerHTML = newobj.d;
                    document.getElementById("num1").value = newobj.d;
                }
            }
        }
    </script>
Posted
Comments
Sanjay K. Gupta 12-Nov-12 5:11am    
What error you have faced and in which line?

1 solution

try following code changes -

1) declare the xmlhttp variable outside the function
JavaScript
var xmlHttp = null;

function CallWcfAjax() {
 //.....
}


2) the callback function for onreadystatechange should either be defined inline or outside the function. So, in your case, without making any changes as given in point 1 above, following will work -
JavaScript
xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4) {
        var obj = null;
        if (val.indexOf("msie") > -1) {
            obj = xmlHttp.responseText;
        }
        else {
            obj = xmlHttp.responseXML;
        }


        var newobj = eval("(function(){return " + obj + ";})()");
        alert(newobj.d);
        result.innerHTML = newobj.d;
        document.getElementById("num1").value = newobj.d;
    }
}


3) alternative for point 2 is to define the function outside -
JavaScript
var xmlHttp = null;

function CallWcfAjax() {
 //.....
 xmlHttp.onreadystatechange = X; 
}

// callback function
function X() {
  //.....
}


Hope this may help you out...

Regards,
Niral Soni
 
Share this answer
 
Comments
pathak vikash 12-Nov-12 6:56am    
Plz look at the code below i have written its working fine in IE but not in Firefox


<script type="text/javascript">
var xmlHttp = null;
function CallWcfAjax() {
var val = navigator.userAgent.toLowerCase();
if (val.indexOf("msie") > -1) {
xmlHttp = new ActiveXObject("Microsoft.XmlHttp");
}
else {
xmlHttp = new XMLHttpRequest();
}

var url = "http://localhost:1912/Service1.svc/ajaxEndpoint/";
url = url + "Sum2Integers";
var body = '{"n1":';
body = body + document.getElementById("num1").value + ',"n2":';
body = body + document.getElementById("num2").value + '}';

//Send HTTP request
xmlHttp.open("POST", url, true);
//create result handler
xmlHttp.onreadystatechange = X;
xmlHttp.setRequestHeader("Content-type", "application/json");
xmlHttp.send(body);
}
function X() {
var val = navigator.userAgent.toLowerCase();
if (xmlHttp.readyState == 4)
{
var obj;
if (val.indexOf("msie") > -1) {
obj = xmlHttp.responseText;
}
else {
obj = xmlHttp.responseText;
}
var newobj = eval("(function(){return " + obj + ";})()");
alert(newobj.d);
result.innerHTML = newobj.d;
}
}

</script>
Niral Soni 12-Nov-12 7:16am    
try interchanging the code sequence -

//create result handler
xmlHttp.onreadystatechange = X;
xmlHttp.setRequestHeader("Content-type", "application/json");
//Send HTTP request
xmlHttp.open("POST", url, true);
xmlHttp.send(body);


The third parameter in xmlHttp.open() indicates that you are trying to invoke asynchronous call (having value set to true). there is different interpretation of this call for mozilla. hence. the open() method should be followed by send() method.

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