Ajax提交有问题吗?

发布于 2024-09-03 17:19:51 字数 1035 浏览 1 评论 0原文

我正在使用 Ajax 将数据发布到服务器(PHP 代码)并更新它。我一个接一个地发布许多数据,但在 Ajax 发布之间失败并且不将 readState 返回到 4。 代码如下

function getHttpRequest()
{
   var request=false;
   if(window.XMLHttpRequest)
   {
       request=new XMLHttpRequest();
   }
   else if(window.ActiveXObject)
   {
       try
       {
           request=new ActiveXObject("Msxml2.XMLHTTP");
       }
       catch(e)
       {
           try
           {
               request=new ActiveXObject("Microsoft.XMLHTTP");
           }
           catch(e)
           {
               request=false;
           }
       }
   }

   return request;
}

代码从这里开始..

function updateAnswer(){
var request=getHttpRequest();
request.open('post','addAnswer.php');
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
request.send("answer="+ans);

if(request.readyState == 4)
{
    var response=request.responseText;
    document.getElementById("display").innerHTML=response;
}
}

我调用此函数来更新数据库中的答案,但有时它不返回 status=4...请帮助

I am using Ajax to post data to the server(PHP code) and update it. i am posting many data one after the other, but in between the Ajax post fails and dont return a readyState to 4.
the code is as follows

function getHttpRequest()
{
   var request=false;
   if(window.XMLHttpRequest)
   {
       request=new XMLHttpRequest();
   }
   else if(window.ActiveXObject)
   {
       try
       {
           request=new ActiveXObject("Msxml2.XMLHTTP");
       }
       catch(e)
       {
           try
           {
               request=new ActiveXObject("Microsoft.XMLHTTP");
           }
           catch(e)
           {
               request=false;
           }
       }
   }

   return request;
}

the code begins here..

function updateAnswer(){
var request=getHttpRequest();
request.open('post','addAnswer.php');
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
request.send("answer="+ans);

if(request.readyState == 4)
{
    var response=request.responseText;
    document.getElementById("display").innerHTML=response;
}
}

i call this function to update answer in database but it donot return status=4 sometimes... please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘虞初梦 2024-09-10 17:19:51

innerinnerHTML 应为 innerHTML。每次 readyState 从 0 变为 4 时,都会调用 updateAnswer。四个是满载的,而较少的则是不同的装载阶段。

innerinnerHTML should be innerHTML. updateAnswer gets called each time readyState is changing from zero to four. Four is fully loaded, while those lesser are different loading stages.

夏见 2024-09-10 17:19:51

为什么不使用像 jQuery 这样的框架来让生活更轻松呢?

另外,您不能一次发布所有数据,这样可以节省与服务器的几次往返吗?

Why not make life easier and use a framework like jQuery?

Also, can't you post all the data at once, in that way save a few roundtrips to the server?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文