ajax发送两次请求

发布于 2024-10-28 04:38:58 字数 1083 浏览 1 评论 0原文

示例网址: http://twitter.realgamingreview.com/index.php

编辑< /strong>:忘记提及:使用测试登录:test/test 作为用户名/密码。

我正在尝试执行一个简单的 AJAX 请求来从数据库中检索一些数据。目标文件 serverTime.php 似乎工作正常;它插入所需的数据并返回所需的响应文本。

然而,该请求似乎被触发了两次。当我使用 Firebug 单步执行 JavaScript 时,这一点很清楚。这会导致页面“重置”(不完全确定),这样我的光标就会从当前文本框中失去焦点,这是一个新问题。该 URL 还显示“localhost/twitter/index.php?message=”,即使我的消息实际上不是空的。我想在出现重大问题之前解决这个相当小的问题。

JavaScript 如下。 ajaxRequest 是我的 XMLHTTPRequest 对象。任何帮助表示赞赏!

//Create a function that will receive data sent form the server
ajaxRequest.onreadystatechange = function(){
    if (ajaxRequest.readyState == 4){
        document.getElementById('output').innerHTML = ajaxRequest.responseText;
    }
}
// build query string
var message = document.myForm.message.value;
var queryString = "message=" + message;

    //send AJAX request
    ajaxRequest.open("GET", "serverTime.php" + "?" + queryString, true);

    ajaxRequest.send(null);

谢谢,

帕拉贡

Example URL: http://twitter.realgamingreview.com/index.php

Edit: forgot to mention: use the test sign in: test/test for username/password.

I am attempting to do a simple AJAX request to retrieve some data from a database. The target file, serverTime.php, seems to be working perfectly; it inserts the desired data and returns the desired responseText.

However, the request seems to be firing twice. This is clear when I step through the JavaScript using Firebug. This causes the page to 'reset' (not exactly sure), such that my cursor loses focus from its current textbox, which is a new problem. The URL also says, "localhost/twitter/index.php?message=", even if my message is not actually empty. I want to fix this fairly minor problem before something major comes of it.

The JavaScript is below. ajaxRequest is my XMLHTTPRequest object. Any help is appreciated!

//Create a function that will receive data sent form the server
ajaxRequest.onreadystatechange = function(){
    if (ajaxRequest.readyState == 4){
        document.getElementById('output').innerHTML = ajaxRequest.responseText;
    }
}
// build query string
var message = document.myForm.message.value;
var queryString = "message=" + message;

    //send AJAX request
    ajaxRequest.open("GET", "serverTime.php" + "?" + queryString, true);

    ajaxRequest.send(null);

Thanks,

Paragon

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

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

发布评论

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

评论(3

夏了南城 2024-11-04 04:38:58

我已经见过很多次了,对我来说,这一直是萤火虫。尝试关闭 firebug 并再次提交请求。使用 fiddler 或其他方式来验证请求仅执行一次。

I've seen this many times, and for me it's always been firebug. Try TURNING OFF firebug and submit the request again. Use fiddler or some other means to verify the request only executed once.

月隐月明月朦胧 2024-11-04 04:38:58

当我用 Javascript 编写 AJAX 函数时,我通常会保留一个状态变量,以防止在当前请求正在进行时分派新请求。如果您只想忽略在另一个请求完成之前发出的请求,您可以执行以下操作:

  1. 将 inProgress 初始化为 false。
  2. 在调用 ajaxRequest.send() 之前将 inProgress 设置为 true。除非 inProgress 为 false,否则不要调用 ajaxRequest.send()。
  3. 当状态为 4 时,ajaxRequest.onreadystatechange() 将 inProgress 设置为 false。

但是,在某些情况下,您希望对操作进行排队。如果是这种情况,那么当 inProgress 为 true 时,您不能忽略对 ajaxRequest.send() 的请求。以下是我针对这些情况的建议:

  1. 将 ajaxQueue 初始化为空的全局数组。
  2. 在调用 ajaxRequest.send() 之前,将请求推送到 ajaxQueue 上。
  3. 在ajaxRequest.onreadystatechange()中当状态为4时,弹出数组以移除刚刚的请求服务。然后,如果 ajaxQueue 不为空(array.size > 0),则再次弹出并对返回的对象调用 send() 。

When I write AJAX functions in Javascript, I usually keep around a state variable that prevents a new request from being dispatched while one is currently in progress. If you just want to ignore requests that are made before another one finishes, you can do something like this:

  1. Initialize inProgress to false.
  2. Set inProgress to true right before calling ajaxRequest.send(). Do not call ajaxRequest.send() unless inProgress is false.
  3. ajaxRequest.onreadystatechange() sets inProgress to false when the state is 4.

In some cases, however, you'd like to queue the actions. If this is the case, then you can't just ignore the request to ajaxRequest.send() when inProgress is true. Here's what I recommend for these cases:

  1. Initialize ajaxQueue to an empty global array.
  2. Before calling ajaxRequest.send(), push the request onto ajaxQueue.
  3. In ajaxRequest.onreadystatechange() when the state is 4, pop the array to remove the request just services. Then, if ajaxQueue is not empty (array.size > 0), pop again and call send() on the object returned.
神经暖 2024-11-04 04:38:58

我的问题与 AJAX 完全无关。相反,这是一个简单(但晦涩)的问题,我的表单中有两个文本框,我可以按 Enter 键并且不会重新加载页面,但只有一个文本框,页面会因某种原因重新加载。

此后,我更改了我的事件系统,这样我就不再依赖于如此不可靠的东西(现在使用 jQuery 来监听特定文本框按下的 Enter 键)。

感谢那些花时间回答我的错误问题的人。

My issue was completely unrelated to AJAX. Instead, it was a simple (but obscure) issue where with two textboxes in my form, I was able to hit enter and not have the page reload, but with only one, the page would reload for some reason.

I have since changed my event system such that I am not relying on something so unreliable (now using jQuery to listen for the Enter key being pressed for specific textboxes).

Thanks to those of you who took the time to answer my misinformed question.

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