使用 jQuery 进行异步 AJAX 查询

发布于 2024-09-26 14:16:24 字数 1112 浏览 4 评论 0原文

我如何通过 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 技术交流群。

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

发布评论

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

评论(4

z祗昰~ 2024-10-03 14:16:24

jQuery 不支持对readyStates 的“本机”(jQuery'ish)访问。

例如,没有可以代表 readyState===3交互式回调。

无论如何,您可以从 .ajax() 访问 成功回调 中的 responseText

$.ajax({
   url:      'some.pl',
   dataType: 'text',
   type:     'GET',
   success:  function(data){
      // data represents xhr.responseText here
   }
});

无论如何,.ajax() 方法返回您可以在必要时访问的 XMLHttpRequest

var myxhr = $.ajax({});
myxhr._onreadystatechange = myxhr.onreadystatechange;

myxhr.onreadystatechange = function(){
    myxhr._onreadystatechange();
    if(myxhr.readyState === 3) {}  // or whatever
};

这是该问题的可能解决方法。但一般来说,您将在 ajax 事件回调中获得所需的所有数据和信息。
此外,XMLHttpRequest 对象 被传递到许多回调中,例如 beforeSenderrorsuccess

有关更多详细信息,请参阅 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 a readyState===3.

Anyway, you have access to the responseText in the success callback from .ajax()

$.ajax({
   url:      'some.pl',
   dataType: 'text',
   type:     'GET',
   success:  function(data){
      // data represents xhr.responseText here
   }
});

Anyway, the .ajax() method returns the XMLHttpRequest which you can access if necessary.

var myxhr = $.ajax({});
myxhr._onreadystatechange = myxhr.onreadystatechange;

myxhr.onreadystatechange = function(){
    myxhr._onreadystatechange();
    if(myxhr.readyState === 3) {}  // or whatever
};

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, like beforeSend, error and success.

See http://api.jquery.com/jQuery.ajax/ for further details.

污味仙女 2024-10-03 14:16:24

为了回答你的第一个问题,成功回调函数需要几个参数,其中一个是返回数据,所以:

$.ajax({url: 'path/to/file.php', async: true, success: function(data){
    // data
}, whileLoading: function(){
    // there is no whileLoading callback function
}});

为了回答你的第二个问题,没有这样的回调函数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:

$.ajax({url: 'path/to/file.php', async: true, success: function(data){
    // data
}, whileLoading: function(){
    // there is no whileLoading callback function
}});

To answer your second question, there is no such callback function whileLoading. See the documentation for more info: http://api.jquery.com/jQuery.ajax/

淤浪 2024-10-03 14:16:24

$.ajax() 返回它生成的 XmlHttpRequest,因此您可以通过这种方式访问​​它,例如:

var xhr = $.ajax({url: 'path/to/file.php', async: true, success: function(data){
    // use data here for the response
}});
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4 && xhr.status == 200) {
    $('#statusDisplay').html(xhr.responseText);
  } else if (xhr.readyState >= 1 && xhr.status == 200) {
    $('#statusDisplay').html('<img src="ajax_load.gif" />');
  }
};

您可能想要的是 beforeSendsuccess< 中的 data (拳头参数) /代码>,像这样:

$.ajax({
  url: 'path/to/file.php', 
  beforeSend: function() { 
    $('#statusDisplay').html('<img src="ajax_load.gif" />');
  },
  success: function(data) {
    $('#statusDisplay').html(data);
  }
});

$.ajax() returns the XmlHttpRequest it generates, so you can access it that way, for example:

var xhr = $.ajax({url: 'path/to/file.php', async: true, success: function(data){
    // use data here for the response
}});
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4 && xhr.status == 200) {
    $('#statusDisplay').html(xhr.responseText);
  } else if (xhr.readyState >= 1 && xhr.status == 200) {
    $('#statusDisplay').html('<img src="ajax_load.gif" />');
  }
};

That what you probably want is beforeSend and the data (fist parameter) in success, like this:

$.ajax({
  url: 'path/to/file.php', 
  beforeSend: function() { 
    $('#statusDisplay').html('<img src="ajax_load.gif" />');
  },
  success: function(data) {
    $('#statusDisplay').html(data);
  }
});
眼泪都笑了 2024-10-03 14:16:24

从 jQuery 1.5 开始,$.ajax() 返回一个 jqXHR 对象< /a>.

这意味着:

没有onreadystatechange机制
然而,前提是既然成功了,
错误、完整和状态代码覆盖
所有可以想到的要求

和 jAndy 的解决方案都将持续更长时间。

我知道没有办法从 jqXHR 访问 XMLHttpRequest 对象。因此,如果您希望响应请求过程中的更改,并且您正在使用 jQuery 1.5,那么看起来您最好依赖 jqXHR 的方法。

As of jQuery 1.5, $.ajax() returns a jqXHR object.

This means that:

No onreadystatechange mechanism is
provided, however, since success,
error, complete and statusCode cover
all conceivable requirements

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.

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