Javascript 我无法让ajax显示/运行
我不熟悉ajax。我无法理解为什么我的代码被我的浏览器忽略。我使用xampp作为服务器,但在我看来,问题与我的浏览器和XMLHttpRequest有关。我不确定,只是想在我的浏览器上显示我的ajax,以便取得显著进展 这是我的密码:Javascript 我无法让ajax显示/运行,javascript,ajax,Javascript,Ajax,我不熟悉ajax。我无法理解为什么我的代码被我的浏览器忽略。我使用xampp作为服务器,但在我看来,问题与我的浏览器和XMLHttpRequest有关。我不确定,只是想在我的浏览器上显示我的ajax,以便取得显著进展 这是我的密码: var xmlHttp = createXmlHttpRequestObject(); function createXmlHttpRequestObject(){ var xmlHttp; if(window.ActiveXObject){
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject(){
var xmlHttp;
if(window.ActiveXObject){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
xmlHttp = false;
}else{
try{
xmlHttp = new XMLHttpRequest();
}catch(e){
xmlHttp = false;
}
}
}
if(!xmlHttp){
alert('Cant create that object hoss');
}else{
return xmlHttp;
}
}
function process(){
if(xmlHttp.readyState == 0 || xmlHttp.readyState == 4){
food = encodeURIComponent(document.getElementById("userInput").value);
xmlHttp.open("GET","index.php?food=" + food,true);
xml.onreadystatechange = handleServerResponse;
xmlHttp.send(null);
}else{
setTimeout('process()',1000);
}
}
function handleServerResponse(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
xmlResponse = xmlHttp.responseXML;
xmlDocumentElement = xmlResponse.documentElement;
message = xmlDocumentElement.firstChild.data;
document.getElementById('underInput').innerHTML =
"<span style='color:blue'>" + message + "</span>";
setTimeout('process()',1000);
}else{
alert('Something went wrong!');
}
}
}
var xmlHttp=createXmlHttpRequestObject();
函数createXmlHttpRequestObject(){
var-xmlHttp;
if(window.ActiveXObject){
试一试{
xmlHttp=新的ActiveXObject(“Microsoft.xmlHttp”);
}捕获(e){
xmlHttp=false;
}否则{
试一试{
xmlHttp=新的XMLHttpRequest();
}捕获(e){
xmlHttp=false;
}
}
}
如果(!xmlHttp){
警报(“无法创建对象hoss”);
}否则{
返回xmlHttp;
}
}
函数过程(){
if(xmlHttp.readyState==0 | | xmlHttp.readyState==4){
food=encodeURIComponent(document.getElementById(“userInput”).value);
open(“GET”,“index.php?food=“+food,true”);
xml.onreadystatechange=handleServerResponse;
xmlHttp.send(空);
}否则{
setTimeout('process()',1000);
}
}
函数handleServerResponse(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
xmlResponse=xmlHttp.responseXML;
xmlDocumentElement=xmlResponse.documentElement;
message=xmlDocumentElement.firstChild.data;
document.getElementById('underInput')。innerHTML=
“+消息+”;
setTimeout('process()',1000);
}否则{
警惕(“出了什么事!”);
}
}
}
我真的需要你的帮助。我不打算分析这段代码。我可以从第一块看出,这是一种旧方法(ciraca 2000),在任何现代浏览器中都不起作用。只需使用一个框架(比如jQuery)来处理AJAX。您是否看到了这些
警报
消息?浏览器控制台中的任何内容?此外,如果您使用的是现代浏览器,您可能会发现API更容易,例如fetch('index.php?food='+food)
不,我没有看到任何警告消息。就好像我的html页面没有任何链接,你能告诉我如何在代码中使用FetchAPI吗?@Phil