2012-06-25 14 views
3

我正在嘗試開發一個phonegap應用程序,它將調用我的coldfusion服務器並將數據返回給手機應用程序。我見過一些教程沒有解釋服務器端的代碼(.cfc文件)。就像這個... http://blog.ryanvikander.com/index.cfm/2012/3/28/Returning-Dynamic-Content-In-A-PhoneGap-App使用Coldfusion作爲後端檢索數據的Phonegap

我希望有人能提供一些示例代碼,當我的服務器上的.cfc接收數據請求時的樣子。

回答

9

下面是該鏈接的示例代碼:

<!doctype html> 
<html> 
    <head> 
     <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width" /> 
     <title>My App</title> 
     <script src="phonegap.js" type="text/javascript" charset="utf-8"></script> 
     <script src="jquery.js" type="text/javascript" charset="utf-8"></script> 

     <script type="text/javascript"> 
      jQuery(document).ready(function() { 
       $.ajax({ 
        url:"http://www.ryanvikander.com/test.cfc?method=getdata", 
        success: function(data){ 
         var content = $("#content"); 

         content.html(data); 
        }, 
        error: function(e){ 
         console.log(e); 
        } 
       }); 
      }); 
     </script> 

    </head> 
    <body> 
     <div id="title_bar">Test</div> 
     This is a test 
     <div id="content"></div> 
    </body> 
</html>

的網址http://www.ryanvikander.com/test.cfc?method=getdata不必是任何比這更:

<cfcomponent> 

    <cffunction name="getdata" access="remote" output="false"> 

     <cfreturn "Hello World!" /> 

    </cffunction> 


</cfcomponent>

這裏,success回調函數將會把字符串"Hello World!"分成<div id="content"></div>

如果功能renderdata()返回查詢,您可以傳遞查詢字符串參數returnformat=json和可選的queryFormat=column以使ColdFusion將查詢數據轉換爲JSON。如果返回的數據使用JSON格式化,那麼您可以遍歷該數據以在任何網站中呈現HTML。

+0

謝謝!一旦我將「access =」public「」改爲「access =」remote「」,它就起作用了。現在我可以從那裏去。 –

+0

哎呀!修復了訪問值。 –

相關問題