代码之家  ›  专栏  ›  技术社区  ›  arik

带jquery的异步Ajax查询

  •  1
  • arik  · 技术社区  · 14 年前

    如何通过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();
    

    我主要感兴趣的是如何检索readystata和状态,以及如何从这些函数中检索响应文本(或多或少类似于此):

    $.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?
    }});
    

    事先谢谢!

    4 回复  |  直到 13 年前
        1
  •  2
  •   jAndy    14 年前

    interactive readyState===3

    responseText success callback .ajax()

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

    XMLHttpRequest

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

    ajax event callbacks
    XMLHttpRequest object beforeSend error success

    http://api.jquery.com/jQuery.ajax/

        2
  •  2
  •   Igor Pavelek    14 年前

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

    . 有关详细信息,请参阅文档: http://api.jquery.com/jQuery.ajax/

        3
  •  0
  •   Nick Craver    14 年前

    $.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" />');
      }
    };
    

    你可能会这样 希望 beforeSend 以及 data (第一个参数)输入 success ,像这样:

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

    从jquery 1.5开始, $.ajax() 返回A jqXHR object .

    这意味着: