2012-07-11 18 views
31

jQueryを使用してajaxリクエストを発生させると、レスポンスヘッダーにどのようにアクセスできますか?いくつかのサイトで提案されているとおり、以下のコードを試してみました。しかし、xhrオブジェクトはnullになります。この文脈ではxhrオブジェクトがあります。しかし、応答ヘッダーにアクセスする方法はありません。 XMLHttpRequestとの下位互換性のためjQuery - AJAXレスポンスヘッダーを取得

function SampleMethod(){ 
    var savedThis=this; 
     this.invokeProcedure=function(procedurePath){ 
      $.ajax({ 
        type: "GET", 
        url: procedurePath, 
        dataType: "json", 
        success: function(data,status,xhr){savedThis.resultSetHandler(data,status,xhr);} 
       }); 
     } 

     this.resultSetHandler=function(data,status,xhrObj){ 
      //Handle the result 
     } 

     this.errorHandler=function(args){ 
      //Handle the result 
     } 

    } 

var sampleObj=new SampleMethod(); 
sampleObj.invokeProcedure('url'); 

答えて

64

、jqXHRオブジェクトは 次のプロパティとメソッドを公開する:getAllResponseHeaders()getResponseHeader()。 jQueryのためにhttp://api.jquery.com/jQuery.ajax/

> 1.3

success: function(res, status, xhr) { 
    alert(xhr.getResponseHeader("myHeader")); 
} 
:$のアヤックス()のドキュメントから
関連する問題