2010-10-07 94 views
6

我想要開始使用SmartGwt。我正在使用XJSONDatasource對具有JSON數據的SmartClient中的示例頁面進行跨域調用。但是,當我運行代碼時,出現一個彈出窗口,其中顯示「查找與您的標準匹配的記錄...」這永遠不會消失,並且數據不會被加載。我正在使用SmartGwt的免費版本(我的公司曾表示這是我們將使用的)。希望我只是錯過了一些明顯的東西。SmartGwt - 從JSON加載網格數據

DataSource dataSource = new XJSONDataSource(); 
    dataSource.setDataTransport(RPCTransport.SCRIPTINCLUDE); 
    dataSource.setDataFormat(DSDataFormat.JSON); 

    dataSource.setDataURL("http://www.smartclient.com/smartgwt/showcase/data/dataIntegration/json/contactsData.js"); 
    DataSourceTextField nameField = new DataSourceTextField("name", "name"); 

    nameField.setValueXPath("name"); 

    dataSource.setFields(nameField); 

    ListGrid grid = new ListGrid(); 
    grid.setDataSource(dataSource); 
    grid.setWidth100(); 
    grid.setHeight(100); 
    grid.setAutoFetchData(true); 
    grid.draw(); 
+0

你應該標記與X-JSON這個問題,以及 – anataliocs 2011-02-10 20:34:08

回答

4

我從文檔,請參閱:http://www.smartclient.com/smartgwt/javadoc/com/smartgwt/client/data/XJSONDataSource.html

注意,如上面的教程 表示,該服務器負責 寫出不僅僅是數據,而是 也是一個JavaScript函數呼叫 告訴客戶,響應已到達 。客戶端將名稱爲 的函數調用爲「回調」 的URL參數。

但有在頁面代碼中沒有這樣的回調你www.smartclient.com/smartgwt/showcase/data/dataIntegration/json/contactsData.js

3

鏈接時,你的SmartGWT的數據源使得調用這個URL:

http://www.smartclient.com/smartgwt/showcase/data/dataIntegration/json/contactsData.js 

(如果數據源正在爲一個Java應用程序的調用到這個答案的底部)

您的數據源將包括一個GET參數請求被稱爲回調看起來像這樣:

callback=isc.Comm._scriptIncludeReply_0 

腳本,contactsData.js,就需要抓住這個GET參數。

contactsData。JS將需要包括圖書館檢索來自URL參數:

的javascript取得的參數功能:

function getParameter (queryString, parameterName) { 
    // Add "=" to the parameter name (i.e. parameterName=value) 
    var parameterName = parameterName + "="; 
    if (queryString.length > 0) { 
     // Find the beginning of the string 
     begin = queryString.indexOf (parameterName); 
     // If the parameter name is not found, skip it, otherwise return the value 
     if (begin != -1) { 
     // Add the length (integer) to the beginning 
     begin += parameterName.length; 
     // Multiple parameters are separated by the "&" sign 
     end = queryString.indexOf ("&" , begin); 
     if (end == -1) { 
     end = queryString.length 
     } 

jQuery的檢索參數功能

http://ajaxcssblog.com/jquery/url-read-request-variables/ 

後你得到的回調參數的值您將使用JSON編寫函數名稱作爲響應正文中的參數,如下所示:

isc.Comm._scriptIncludeReply_0({"item": [ {"id": "1","name": "Monkey"}, 
{"id": "2","name": "Tree"}, 
{"id": "3","name": "Banana"} ] }) 

所以javascript代碼會是這個樣子:

Response.Write(getParameter(URLrequestFromDatasourceString,"callback") + " (" + JSON + ")"); 

JAVA

如果您SmartGWT的數據源進行調用以Java應用程序URL:

http://www.smartclient.com/getJson.htm

你的Java控制器會做同樣的事情,但它是mu CH容易

String callbackString = request.getParameter("callback"); 

response.setContentType("text/X-JSON"); 
response.getWriter().write(callbackString + " (" + JSONstring + ") "); 
response.setStatus(HttpServletResponse.SC_OK); 

Also, here is a link to a blog post on the issue