2012-08-31 39 views
0

我有我的Facebook共享scritp:如何從數據庫中獲取價值,並插入JavaScript函數裏面

<script> 
    $(document).ready(function(){ 

     $('#share_button').click(function(e){ 
      e.preventDefault(); 
      FB.ui(
      { 
       method: 'feed', 
       name: 'This is the content of the "name" field.', 
       link: ' http://www.hyperarts.com/', 
       picture: 'http://www.hyperarts.com/external-xfbml/share-image.gif', 
       caption: 'This is the content of the "caption" field.', 
       description: 'This is the content of the "description" field, below the caption.', 
       message: '' 
      }); 
     });  

    }); 
    </script> 

但是,我想要得到「名」,「鏈接」,「圖片報」的價值觀和其他人從數據庫,也許是一個簡單的訪問mbd文件。我將使用經典的ASP與此數據庫的連接...

有一種方法可以做到這一點?

+0

此數據庫位於何處? – Aesthete

+0

在我的網頁相同的文件夾...或在一個文件夾caled「數據庫」..位置無所謂...我想... – Preston

回答

0

您需要創建一個服務器端腳本(例如使用php/mysql或asp.net等),從數據庫中讀取所需的值並以JSON格式輸出它們,然後使用ajax使用從服務器cherrypy

import cherrypy 
import json 

names = ['Bob'] // List of names. You call fill this from a DB 
class BasicServer: 
    @cherrypy.expose 
    def names(self): 
     return json.dumps(names) // Returns a JSON list of names from the DB. 

cherrypy.quickstart(BasicServer()) 

真的,真的基本要求拿到劇本的JSON輸出,並使用該響應值來創建你的FB對象

0

真的,真的簡單的Python服務器。

$('#share_button').click(function(e){ 
    e.preventDefault(); 
    var names = null; 
    // Request all the data you want from the server. 
    $.when($.get('serverAddress/names', function(data) { names = data; })) 
     // When all your requests are done, call your FB.ui function. 
     .done(FB.ui({name: names[0]}); // Just selects the first name ('bob') 
} 

它真的聽起來像你想要的是某種模板引擎。這樣,您可以爲您的頁面提供數據預填充。由於這裏的示例是python,請查看庫如MAKOmustache

+0

感謝您的答案,但我使用經典ASP來獲取值從數據庫。我不知道「python」... :) – Preston

相關問題