1

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

前もって感謝します!

4

4 に答える 4

2

jQueryは、readyStatesへの「ネイティブ」(jQuery'ish)アクセスをサポートしていません。

interactiveたとえば、を表すことができるコールバックはありませんreadyState===3

とにかく、あなたはからにアクセスresponseTextできsuccess callbackます.ajax()

$.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 event callbacks
さらに、、、などのXMLHttpRequest object多くのコールバックに渡されます。beforeSenderrorsuccess

詳細については、 http://api.jquery.com/jQuery.ajax/を参照してください。

于 2010-10-11T09:22:51.030 に答える
2

最初の質問に答えるために、成功のコールバック関数はいくつかのパラメーターを受け取ります。そのうちの 1 つは返されるデータです。

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

2 番目の質問に答えるために、そのようなコールバック関数whileLoadingはありません。詳細については、ドキュメントを参照してください: http://api.jquery.com/jQuery.ajax/

于 2010-10-11T09:26:19.650 に答える
0

$.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);
  }
});
于 2010-10-11T09:28:04.813 に答える
0

jQuery 1.5 以降、 jqXHR オブジェクト$.ajax()を返します。

この意味は:

ただし、success、error、complete、および statusCode は考えられるすべての要件をカバーするため、onreadystatechange メカニズムは提供されません。

jAndyのソリューションは、より長く機能します。

jqXHR から XMLHttpRequest オブジェクトにアクセスする方法を知りません。したがって、リクエストの過程で変更に対応しようとしていて、jQuery 1.5 を使用している場合は、jqXHR のメソッドに頼るのが最善のようです。

于 2011-03-17T18:14:49.503 に答える