2012-04-02 86 views
-1

我正在構建一個氣泡工具提示,當光標懸停在用戶的名字上時,它應該顯示用戶配置文件信息。我已經使用硬編碼文本工作,但是當我嘗試使用AJAX請求使用動態數據填充工具提示時,它只是不返回任何內容。使用AJAX從文件檢索數據

要檢索的數據位於文件process.cfm中。該文件是這樣的:

<div class="personPopupResult">Test text</div> 

調用該文件的代碼如下:

$.ajax({ 
    type: 'GET', 
    url: 'process.cfm', 
    data: '?id=' + currentID, 
    success: function(data) { 
     var text = $(data).find('.personPopupResult').html(); 
     $('#personPopupContent').html(text); 
    } 
}); 

任何想法,爲什麼這不起作用,將不勝感激。

謝謝。

編輯:

$.ajax({ 
      type: 'GET', 
      url: 'process.cfm?=id' + currentID, 

      success: function(data){ 

       $('#personPopupContent').html(data); 

       if ($(data).find('.personPopupResult').length) { 
        var text = $(data).find('.personPopupResult').html(); 
        $('#personPopupContent').html(text); 
       } 

      } 
     }); 

代碼的工作,但顯示process.cfm頁面的一切,不僅.personPopupResult股利。

+0

嘗試用這種'變種文字= $(數據).find( 'personPopupResult。')文本();' – Poonam 2012-04-02 07:53:43

+0

嘗試插入警報(文本),VAR文本之後。 ...如果有幫助,試試票。 – Alexandr 2012-04-02 08:35:32

+0

我插入'alert(文本);'但它不顯示。我爲什麼要它顯示文本?目前的問題是如何將輸出限制爲只有一個類。 – Eleeist 2012-04-02 08:53:26

回答

1

試試這個:

$.ajax({ 
    type: 'GET', 
    url: 'process.cfm?=id' + currentID, 

    success: function(data) { 
     //actually here you should better write following 
     //$('#personPopupContent').html(data); 
     // but if not works try this 
     if ($('#personPopupContent').length) 
     { 
      alert(' $(#personPopupContent) not found!'); 
     } 
     if ($(data).find('.personPopupResult').length) 
     { 
     var text = $(data).find('.personPopupResult').html(); 
     $('#personPopupContent').html(data); 
     } 
     else 
     { 
     $('#personPopupContent').html('Ups error here!'); 
     } 
    } 
}); 
+0

不工作。你將「data」參數傳入函數,但是你沒有聲明它。對我來說看起來不太合適。 順便說一句,我應該提到澄清:最終的應用程序將使用id來檢索用戶的數據,但現在我想讓它與文本一起工作。 – Eleeist 2012-04-02 08:06:33

+0

數據不應聲明。它的默認值。數據='23'(等號)? – Alexandr 2012-04-02 08:11:05

+0

在第一篇文章中查看我的編輯。代碼有效,但在process.cfm中顯示所有內容。 – Eleeist 2012-04-02 08:23:08

0

嘗試打印jQuery對象有:

console.log($('#personPopupContent')); 

是否有針對性內元素?

+1

會更好,如果這是一個評論,而不是答案 – 2012-04-02 07:55:29