2017-04-24 3 views
0

を受けた私はxmlhttp.responseJSONを取得することはできませんよ、のJavaスクリプトでスクリプトを以下のJavascript AJAX応答からオブジェクトを取得することができ作業ではなくは

// Below is the Ajax call for get http methods 
function callAjaxGet(url, callback){ 
    var xmlhttp; 
    // compatible with IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp = new XMLHttpRequest(); 
    xmlhttp.onreadystatechange = function(){ 
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200){ 
      console.log('+++++++++++++++++++++++++++'); 
      console.log(xmlhttp['responseJSON']); 
      console.log(xmlhttp.responseJSON); 
      callback(xmlhttp.responseJSON); 
     } 
    } 
    xmlhttp.open("GET", url, true); 
    xmlhttp.send(); 
} 

を試してみましたが、それが応答に移入なっています、私は火かき棒の窓でそれを見ることができます。それにアクセスすることはできません。

私は、次のいずれかを呼び出すことにより、機能の上に使用しています。

window.onload = function() { 
    url = "/user/getAll"; 
    var data = callAjaxGet(url, function(responseJSON) { 
     // on success call back. 
     console.log(responseJSON); 
    }); 
} 

私はこの最後の数時間を探していますが、まだ成功していません。

+0

**何のロギングは動作しません、私は 'undefined'見るけどresponseJSON –

+0

があなたの' .onreadystatechange'を定義すると、私は全体のXMLHTTPオブジェクトを印刷する場合、私は、ロギングでそれを見ることができます(xmlhttp.readyState == 4 && xmlhttp.statusの== 200){ はconsole.log( '場合**の後には '(...).open使用して' ... – KarelG

+0

@KarelGは –

答えて

0

これを試してみてください:

// Below is the Ajax call for get http methods 
function callAjaxGet(url, callback){ 
    // compatible with IE7+, Firefox, Chrome, Opera, Safari 
    var xmlhttp = new XMLHttpRequest(); 
    xmlhttp.onreadystatechange = function(){ 
     if (this.readyState == 4 && this.status == 200){ 

      callback(JSON.parse(this.responseText)); 
     } 
    } 
    xmlhttp.open("GET", url, true); 
    xmlhttp.send(); 
} 
+0

なぜJSON.parse(this.responseText)のいずれかを使用しますか? this.responseJSONだけを使用することはできませんか?私はこれを試してみました。私は@AndreDionを推測 –

+0

は私の質問:)感謝:) –

+0

私は上記のコメントであなたを尋ねた質問の意味を答える、私はあなたの答えが正しいものであると思います。 :) –

関連する問題