xmlhttp是未定义的.Javascript Ajax [英] xmlhttp is undefined. Javascript Ajax

查看:67
本文介绍了xmlhttp是未定义的.Javascript Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是javascript的新手,尤其是ajax..只是想弄清楚..

I am new to javascript and especially to ajax..And just trying to figure it out..

我从一个教程中编写了这段代码,找不到我做错了什么.在这里您可以实时看到它

i wrote this code from one tutorial and can't find what i did wrong. Here you can see it live

我从Firebug收到的错误:"TypeError:xmlhttp未定义[解决此错误]

The error i am getting from Firebug: "TypeError: xmlhttp is undefined [Break On This Error]

如果(xmlhttp.readyState == 4){"

if (xmlhttp.readyState == 4){"

我的代码是

// JavaScript Document

var xmlhttp;
var url;

function ajaxFunction(){

if  (window.ActiveXObject){//if the window is InternetExplorer

    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

    }else if(window.XMLHttpRequest){// if Window is Firefox etc..

        xmmlhttp= new XMLHttpRequest();

        }else{

            alert ("Get a New Browser")

            }

}//end of ajaxFunction()


function getInfo(){

    ajaxFunction();

    var entryInfo= document.getElementById("entry").value;

            function stateChanged(){
                if (xmlhttp.readyState == 4){

                    document.getElementById("results").innerHTML = xmlhttp.responseText;

                    }//if (xmlhttp.readyState == 4)

                }//end of function stateChanged()


url = "info.php?user="+entryInfo;
xmlhttp.onreadystateshange=stateChanged();
xmlhttp.open("GET",url,true);
xmlhttp.send(null);


    }// end of function getInfo

推荐答案

您在这里有错字:

   xmmlhttp= new XMLHttpRequest();
     ^

更改为

 xmlhttp= new XMLHttpRequest();

也正如Michael指出的那样,在分配onreadystatechange函数时,您需要加上括号:

Also as Michael pointed out, you have parenthesis when assigning your onreadystatechange function:

xmlhttp.onreadystateshange=stateChanged();
                                       ^ remove the ()

如果不删除括号,将调用 stateChange()函数,并将返回值提供给您不使用的 xmlhttp.onreadystateshange 不想.

If you don't remove the parenthesis, the stateChange() function will be called, and the return value will be given to xmlhttp.onreadystateshange which you don't want.

这篇关于xmlhttp是未定义的.Javascript Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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