2013-05-05 26 views
1

使用API​​我正在使用Javascript創建論壇。更改URL字符串中的數據 - 使用API​​進行數據庫請求

當用戶點擊index.html頁面上的主題標題,他們被帶到一個叫就到這裏thread.html頁我使用的API調用,以顯示與ID爲線索,結果1

var threadRequest = new Request.JSON({ 
    url: 'control.php?action=getThread&id=1', 
    onSuccess: threadSuccess 
    }).send(); 

我問題是,我將如何改變這種做法,它知道哪些線程已被點擊,所以它知道哪個線程調用,我已經試過

function callThread(thread){ 
    var threadRequest = new Request.JSON({ 
    url: 'control.php?action=getThread&id='+thread, 
    onSuccess: threadSuccess 
    }).send(); 
} 

thread是在鏈接到API表的外鍵。

線程標題顯示,像這樣

var jsonRequest = new Request.JSON({ 
    url: 'control.php?action=getThreadList', 
    onSuccess: ajaxSuccess 

}).send(); 

這是threadSuccess功能以及

function threadSuccess (jArray){ 
    jArray.each(function(post){ 
     var postObject = new PostItem(post.id, post.name, post.comment, post.thread); 
     postObject.display().inject($('postList')); 
    }) 
} 

我的問題是你得到它,這樣的URL字符串會做類似url: 'control.php?action=getThread&id=what ever number thread the user has clicked on

帖子表中包含字段id,名稱,註釋,時間戳和線程(這將定義帖子將在哪個線程中)。

和一個線程表包含ID和標題。

+0

這裏有個問題嗎?當你說你嘗試過時,它不起作用嗎?什麼不起作用?似乎是合理和直接的 - 忽略了「爲什麼」以及你從服務器公開的內容,seo等等。你可以做的一件事是將URL設置爲'control.php',方法爲'get'並將對象作爲'data'傳遞給key-> value對,更容易管理 – 2013-05-05 20:49:34

+0

我的問題是我需要做什麼,它做了類似'url:'control.php?action = getThread&id =用戶點擊過的任何數字線程'換句話說如果用戶點擊線程號5,則會在等號後添加一個5。 – joshuahornby10 2013-05-05 21:55:52

+0

沒有錯誤時,它不工作屏幕只是空白,換句話說查詢失敗。 – joshuahornby10 2013-05-06 08:56:27

回答

0

當你輸出你的鏈接,這樣做:

<div class="thread" data-id="{{ID from database}}">Thread</div> 

document.querySelector('.thread').addEventListener('click', function(e){ 
    var id = e.currentTarget.getAttribute('data-id'); 
    callThread(id); 
}, false); 

你問略有措辭混淆的問題,但是這是我想接近它,如果我試圖實現的方式我認爲你是誰!