使用 jQuery 进行异步 AJAX 查询
我如何通过 jQuery 执行以下操作?
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementByID('statusDisplay').innerHTML = xmlhttp.responseText; // show loaded content
} else if (xmlhttp.readyState >= 1 && xmlhttp.status == 200) /* if(xmlhttp.readyState>=2 && xmlhttp.status==200) */ {
document.getElementByID('statusDisplay').innerHTML = '<img src="ajax_load.gif" />'; // show ajax loading image
}
}
xmlhttp.open("GET", "path/to/file.php", true);
xmlhttp.send();
我主要感兴趣的是如何检索readyStatea和状态以及如何从这些函数中检索响应文本(或多或少像这样):
$.ajax({url: 'path/to/file.php', async: true, success: function(){
// how can I get the responseText here?
}, whileLoading: function(){
// does such a parameter actually exist?
}});
提前致谢!
how can I do the following by means of jQuery?
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementByID('statusDisplay').innerHTML = xmlhttp.responseText; // show loaded content
} else if (xmlhttp.readyState >= 1 && xmlhttp.status == 200) /* if(xmlhttp.readyState>=2 && xmlhttp.status==200) */ {
document.getElementByID('statusDisplay').innerHTML = '<img src="ajax_load.gif" />'; // show ajax loading image
}
}
xmlhttp.open("GET", "path/to/file.php", true);
xmlhttp.send();
What I am mainly interested in is how I can retrieve the readyStatea and status and how I can retrieve the response text from within those functions (more or less like this):
$.ajax({url: 'path/to/file.php', async: true, success: function(){
// how can I get the responseText here?
}, whileLoading: function(){
// does such a parameter actually exist?
}});
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
jQuery 不支持对readyStates 的“本机”(jQuery'ish)访问。
例如,没有可以代表
readyState===3
的交互式
回调。无论如何,您可以从
.ajax()
访问成功回调
中的responseText
无论如何,
.ajax() 方法返回您可以在必要时访问的
XMLHttpRequest
。这是该问题的可能解决方法。但一般来说,您将在
ajax 事件回调
中获得所需的所有数据和信息。此外,
XMLHttpRequest 对象
被传递到许多回调中,例如beforeSend
、error
和success
。有关更多详细信息,请参阅 http://api.jquery.com/jQuery.ajax/。
jQuery does not support a "native" (jQuery'ish) access to readyStates.
There is no
interactive
callback for instance which could represent areadyState===3
.Anyway, you have access to the
responseText
in thesuccess callback
from.ajax()
Anyway, the
.ajax()
method returns theXMLHttpRequest
which you can access if necessary.This is a possible workaround of that issue. But in general you will have all data and information you need within the
ajax event callbacks
.Furthermore is the
XMLHttpRequest object
passed into a lot of callbacks, likebeforeSend
,error
andsuccess
.See http://api.jquery.com/jQuery.ajax/ for further details.
为了回答你的第一个问题,成功回调函数需要几个参数,其中一个是返回数据,所以:
为了回答你的第二个问题,没有这样的回调函数whileLoading。有关详细信息,请参阅文档: http://api.jquery.com/jQuery.ajax/
To answer your first question, success callback function takes few parameters, one of them is returned data, so:
To answer your second question, there is no such callback function whileLoading. See the documentation for more info: http://api.jquery.com/jQuery.ajax/
$.ajax()
返回它生成的 XmlHttpRequest,因此您可以通过这种方式访问它,例如:您可能想要的是
beforeSend
和success< 中的
data
(拳头参数) /代码>,像这样:$.ajax()
returns the XmlHttpRequest it generates, so you can access it that way, for example:That what you probably want is
beforeSend
and thedata
(fist parameter) insuccess
, like this:从 jQuery 1.5 开始,
$.ajax()
返回一个 jqXHR 对象< /a>.这意味着:
和 jAndy 的解决方案都将持续更长时间。
我知道没有办法从 jqXHR 访问 XMLHttpRequest 对象。因此,如果您希望响应请求过程中的更改,并且您正在使用 jQuery 1.5,那么看起来您最好依赖 jqXHR 的方法。
As of jQuery 1.5,
$.ajax()
returns a jqXHR object.This means that:
and jAndy's solution will on longer work.
I know of no way to access the XMLHttpRequest object from jqXHR. So if you are looking to respond to changes in the course of a request, and you are using jQuery 1.5, it looks like you had best rely on jqXHR's methods.