我已经多次尝试通过 Ajax 将我的表单数据发送到我的服务器脚本而不刷新页面(使用表单的 onSubmit 事件)。这一切都适用于所有浏览器(Chrome、IE 等),但对于 Firefox,处理过程一直在继续。我的意思是,即使数据已经发送到服务器端(是的,很多时候,我已经在服务器端获得了数据,但客户端仍在处理中),客户端不会响应连续的调用.
例如,考虑我的示例代码之一:
这是Javascript
function submitComment(id)
{
//id is the id of the postbox
var content=$("#"+id).val();
var xmlhttp;
if(window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();
else
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
//action taken in response to the script from server in response to this form submission, eg, enabling back the submit button
$('input[type="submit"]').removeAttr('disabled');
}
}
$('input[type="submit"]').attr('disabled','disabled');
xmlhttp.open("POST",host+"comment.php?mode=newpost",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("post="+content);
$('#status').html('Posting your comment. Please wait...');//shows this status as long as gets no response from server
return false;
}
这是它的 HTML:
<form id='newpostform' method=post action='' onSubmit="return submitComment('post')" >
<textarea id='post' name='post'></textarea>
<input type=submit value=Post>
<span id='status' style='padding:10px; font-size:12px; color:black; font-family:cambria, segoe ui;'>
</span>
</form>
因此,在除 Firefox 之外的所有浏览器中,提交按钮都会被禁用一段时间,直到脚本将帖子传达给服务器并且当服务器响应时,提交按钮被激活并更新状态。
问题正是在 Firefox 中出现的。即使数据已经传送到服务器,状态栏也不会改变其状态!
最佳答案
问题评论的答案:
Got solution to my problem. XMLHTTPRequest.status returns 0 and responseText is blank in FireFox 3.5
It was only due to the fact that I was using absolute path. Using relative path solved my problem.
太棒了!
关于您的附加问题:
However, I'm still confused with the fact that I've to use relative path like this: index/index.php in the path attribute of the script file residing in the index directory itself. Why? I mean, simply using 'index.php' should have solved the problem, isn't it?
据我了解,您的 HTML 位于 http://example.org/test.html与
<script src="subdir/main.js>
...解析为 http://example.org/subdir/main.js ,它调用 XMLHttpRequest:
xhr.open("POST", "index.php", true);
这要求http://example.org/index.php , 不是 http://example.org/subdir/index.php
这是因为 XMLHttpRequest 中的相对 URL(在许多其他情况下)是 resolved反对the document's base URL ,而不是脚本的 URL。
如果根据脚本的 URL 解析相对 URL,它可能会变得非常困惑:假设您在 /lib/jquery.js
有 jQuery,它调用 XMLHttpRequest 和您自己的代码在 /main.js
调用 jQuery.ajax(... "data.txt"...) 。此请求应该请求 /data.txt
还是 lib/data.txt
?
关于javascript - Firefox 问题与 Javascript : Not updating status on form submission,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16309598/