2013-04-11 74 views
0

我知道很多話題都存在,但是我沒有找到解決我的問題的方法。 我做了一個AJAX請求,我想分析結果,並將其注入到我的頁面中的表中。用jquery解析AJAX結果

$.ajax({ 
     type : "GET", 
     url  : "http://keyserver.gingerbear.net/pks/lookup?", 
     data : "[email protected]&fingerprint=on&op=index", 
     dataType: "html", 
     success : function(resultat){ 
         $(resultat).ready(function() { 
           $('pre',$(resultat).html()).each(function(){ 
           var htmlString = $(this).html(); 
           console.log("Key ID : "+/0x[a-z0-9]{16}/i.exec(htmlString)); 
           console.log("User : "+$(this).find('a:eq(1)').text()); 
           console.log("Bits : "+/[0-9]{4}[RD]/.exec(htmlString)); 
           console.log("Data : "+/[0-9]{4}-[0-9]{2}-[0-9]{2}/.exec(htmlString)); 
           console.log("Fingerprint : "+/Fingerprint=(.*)/.exec(htmlString)); 
           }); 
         }); 
        }, 
     // Blabla 
    }); 

我看到here解決方案的開始。例如,我可以做console.log($(resultat).find('a')),我有第一個的內容,但是console.log($(resultat).find('pre'))沒有工作...

+0

你可以給一個'reultat'的樣本值嗎? – nmenego 2013-04-11 14:00:10

+0

是的,resultat包含此頁面的內容,在我的示例中:keyserver.gingerbear.net/pks/[email protected]&fingerprint=on&op=index – 2013-04-11 14:42:18

回答

0

首先,您應該使用jQueryload將外部內容加載到元素中。

要回答你的問題,你不能使用find去尋找pre標籤時$(this)代表您要搜索(在這種情況下pre)的元素。但是,您可以搜索pre的父元素,然後從父元素中使用.find

var parent = $(this).closest("#parentDiv"); 
var preChild = parent.find("pre").first(); 
+0

Woohooo!通過加載函數,我可以真正解析結果並將其顯示在控制檯中!現在我必須用結果構建我的表格,並將其注入DOM而不是粗暴的內容。 – 2013-04-11 14:35:14