gpt4 book ai didi

javascript - xmlhttp 未定义。 JavaScript Ajax

转载 作者:行者123 更新时间:2023-11-28 20:45:14 24 4
gpt4 key购买 nike

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

我从一个教程中编写了这段代码,但找不到我做错了什么。 Here you can see it live

我从 Firebug 收到的错误:“TypeError: xmlhttp 未定义[中断此错误]

如果 (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 函数时有括号:

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

如果您不删除括号,stateChange() 函数将被调用,并且返回值将被赋予您不删除的 xmlhttp.onreadystateshange不想。

关于javascript - xmlhttp 未定义。 JavaScript Ajax ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13585807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com