当前位置: 动力学知识库 > 问答 > 编程问答 >

Having trouble getting ajax to display .php

问题描述:

I'm trying to learn Ajax, but keep running into trouble. Basically I'm having the httpRequest check which browser. Then get the browser to load the .php file into the div.

<script>

function getXMLHTTPRequest(){

var ajaxRequest = false;

try{

ajaxRequest = new XMLHttpRequest();

} catch (e)

{

try

{

ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");

}catch (e){

try{

new ActiveXObject("Microsoft.XMLHTTP");

} catch(e) {

alert("Your browser broke!");

return false;

}

}

}

var ajaxRequest = getXMLHTTPRequest();

ajaxRequest.open("GET","ajaxNewbie.php");

ajaxRequest.onreadystatechange = function ()

{

if(ajaxRequest.readyState === 4)

{

//get the data from the server's response

document.greetingInfo.innerHTML = ajaxRequest.responseText;

} else {

window.alert("readyState = " + ajaxRequest.readyState);

}

}

}

</script>

</head>

<body onload="ajaxRequest.send(null);">

<div id="greetinginfo">

</div>

</body>

网友答案:

You made few errors:

  1. getXMLHTTPRequest function is not complete, so it will be called from itself
  2. you forgot ajaxRequest.send();

Consider following example: http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_get

分享给朋友:
您可能感兴趣的文章:
随机阅读: