Firebug 给出错误“Ajax 未定义”
我有这个 HTML 表单,它使用 AJAX 将数据传递到 php 文件。 (我这样做是为了不必离开当前页面)。 Firebug 给了我两个错误:
document.getElementById("name") is null
ajaxget is not defined
我今天才开始使用 AJAX 是出于绝望,所以我对它还很陌生。 PHP 文件是 Second.php,重要的部分是它接收变量,以便它可以创建一个输出供另一个 html 稍后使用,但它不必响应:
<html>
<head>
</head>
<body>
<script type="text/javascript">
function ajaxRequest(){
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
for (var i=0; i<activexmodes.length; i++){
try{
return new ActiveXObject(activexmodes[i])
}
catch(e){
//suppress error
}
}
}
else if (window.XMLHttpRequest) // if Mozilla, Safari etc
return new XMLHttpRequest()
else
return false
}
var mygetrequest=new ajaxRequest()
mygetrequest.onreadystatechange=function(){
if (mygetrequest.readyState==4){
if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1){
document.getElementById("result").innerHTML=mygetrequest.responseText
}
else{
alert("An error has occured making the request")
}
}
}
var namevalue=encodeURIComponent(document.getElementById("name").value)
var agevalue=encodeURIComponent(document.getElementById("age").value)
mygetrequest.open("GET", "second.php?name="+namevalue+"&age="+agevalue, true)
mygetrequest.send(null)
</script>
<form method="get" action="">
Your name: <input type="text" id="name" name="name" size="25" /> <br />
Your age: <input type="text" id="age" name="age" size="25" /> <br />
<input type="button" value="submit" onClick="ajaxget()" />
</form>
<div id="result"> </div>
</body>
</html>
I have this HTML form which uses AJAX to pass data to a php file. (I did this so I don't have to leave the current page). Firebug gives me two errors :
document.getElementById("name") is null
ajaxget is not defined
I only started using AJAX today out of desperation, so I'm very new to it. The PHP file is second.php
, the important part is that it receives the variables so it can create an output to be used by another html later, but it doesn't have to respond back:
<html>
<head>
</head>
<body>
<script type="text/javascript">
function ajaxRequest(){
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
for (var i=0; i<activexmodes.length; i++){
try{
return new ActiveXObject(activexmodes[i])
}
catch(e){
//suppress error
}
}
}
else if (window.XMLHttpRequest) // if Mozilla, Safari etc
return new XMLHttpRequest()
else
return false
}
var mygetrequest=new ajaxRequest()
mygetrequest.onreadystatechange=function(){
if (mygetrequest.readyState==4){
if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1){
document.getElementById("result").innerHTML=mygetrequest.responseText
}
else{
alert("An error has occured making the request")
}
}
}
var namevalue=encodeURIComponent(document.getElementById("name").value)
var agevalue=encodeURIComponent(document.getElementById("age").value)
mygetrequest.open("GET", "second.php?name="+namevalue+"&age="+agevalue, true)
mygetrequest.send(null)
</script>
<form method="get" action="">
Your name: <input type="text" id="name" name="name" size="25" /> <br />
Your age: <input type="text" id="age" name="age" size="25" /> <br />
<input type="button" value="submit" onClick="ajaxget()" />
</form>
<div id="result"> </div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在任何函数之外都有
var namevalue=encodeURIComponent(document.getElementById("name").value)
。因此,它在解析元素时运行 - 这是在 DOM 中存在具有该 id 的元素之前。
您有
onClick="ajaxget()"
但没有任何地方提及ajaxget
(例如function ajaxget() { …< /代码>
You have
var namevalue=encodeURIComponent(document.getElementById("name").value)
outside of any function. So it runs when the<script>
element is parsed — which is before the element with that id exists in the DOM.You have
onClick="ajaxget()"
but nowhere do you have any other mention ofajaxget
(such asfunction ajaxget() { …
您的代码中没有定义
ajaxget()
函数。You do not have an
ajaxget()
function defined in your code.