Ajax 在 Firefox 中通过 form 触发时以空字符串响应onsubmit
,但在 Internet Explorer 和 Opera 中运行良好(如果通过提交按钮而不是 form 发送,则在 Firefox 中运行onsubmit
)。
我只是用 ajax GET 调用一个 php 文件,用 - 调用 php 文件响应echo $response = "something";
。然后会提醒响应值。我让它在 IE 中工作,但在 Firefox 中,响应是一个空字符串(由 选中typeof()
)。
代码是 4 个文件:index.php、ajax-connect.js、cbb.js、cbb.php
索引.php
<html> <head> <script src="ajax-connect.js"></script> <script src="cbb.js"></script>
</head> <body>
<form name="condtactform1" method="get" action="" onSubmit="contactfunction()">
<input type="submit" name="hissubmit" id="hissubmit" value="submit"> </form>
</body> </html>
ajax-connect.php
/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttp = false;
/*@cc_on @*/
/*@
if (@_jscript_version >= 5)
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
xmlHttp = false;
}
}
@end
@*/
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest();
}
cbb.js
function contactfunction() {
var url = "cbb.php";
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = updatecontact1;
xmlHttp.send(null);
}
function updatecontact1() {
if (xmlHttp.readyState == 4) {
var responsae = xmlHttp.responseText;
alert(responsae);
}
}
cbb.php
<?php $response = "something"; echo $response; ?>
如果我通过提交按钮而不是表单提交来触发 ajax,那么它在 Firefox 中可以正常工作,例如:
<form name="condtactform1" method="get" action="">
<input type="submit" name="hissubmit" id="hissubmit" value="submit" onFocus="contactfunction()"> </form>
知道为什么要这样做吗?
谢谢。