javascript - Firefox 问题与 Javascript : Not updating status on form submission

标签 javascript forms firefox onsubmit

我已经多次尝试通过 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/

相关文章:

javascript - 使用 jquery.html() 将 html 元素保存到数据库

php - 如何将 URL 复制到我的表单中

java - Firefox 无法使用 FirefoxDriver Selenium 打开

javascript - jQuery - 无法选择最后一个选项组的第一个选项

javascript - webRTC 音频/语音

javascript - 延迟 window.print() 直到页面在 ajax 请求中加载以避免空白打印屏幕?

javascript - 无法返回 xmlhttp.responseText?

ruby-on-rails - 缺少注册/创建、应用/创建的模板

html - 一个页面中的多个表单或多个提交?

javascript - 如何在 Firefox 插件中调用控制台调试面板?