遇到Onreadystatechange事件的问题 [英] getting problem with Onreadystatechange event

查看:64
本文介绍了遇到Onreadystatechange事件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请大家看下面的代码,并通过代码更正进行答复.

Plz anybody look at the following code and reply with code correction.

<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>

推荐答案

尝试以下代码更改-

1)在函数外声明xmlhttp变量
try following code changes -

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

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



2)onreadystatechange的回调函数应该内联定义或在函数外部定义.因此,在您的情况下,无需进行以上第1点中所述的任何更改,以下操作将有效-



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 -

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)第2点的替代方法是在-
外部定义函数



3) alternative for point 2 is to define the function outside -

var xmlHttp = null;

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

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



希望这可以帮助您...

问候,
尼拉·索尼(Nial Soni)



Hope this may help you out...

Regards,
Niral Soni


这篇关于遇到Onreadystatechange事件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆