ajax发送两次请求
示例网址: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经见过很多次了,对我来说,这一直是萤火虫。尝试关闭 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.
当我用 Javascript 编写 AJAX 函数时,我通常会保留一个状态变量,以防止在当前请求正在进行时分派新请求。如果您只想忽略在另一个请求完成之前发出的请求,您可以执行以下操作:
但是,在某些情况下,您希望对操作进行排队。如果是这种情况,那么当 inProgress 为 true 时,您不能忽略对 ajaxRequest.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:
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:
我的问题与 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.