2012-05-21 97 views
2

我是GWT和PHP的新手,在閱讀了一些教程之後,我不清楚如何在GWT前端和PHP後端之間高效地交換數據。我成功地遵循了Google教程,其中建議使用RequestBuilder類從PHP獲取數據。 但是當我需要保存在GWT中完成的工作時,如何有效地將數據傳遞給PHP? 我不清楚如何使用RequestBuilder完成此任務。 ,我發現現在的解決方案是在GWT和PHP之間傳遞數據

RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, URL.encode(url)); 
    builder.setHeader("Content-Type", "application/json"); 
     try { 
       Request request = builder.sendRequest("{\"done\":false,\"description\":\"Some text\",\"priority\":0}", new RequestCallback() {... 

和PHP端

$arr = json_decode(file_get_contents("php://input"),true); 
$done = (int)$arr['done']; 
$description = $arr['description']; 
$priority = $arr['priority']; 
mysql_query("INSERT INTO Tasks (done, description, priority) 
    VALUES ($done, '$description', $priority)"); 

這是最好的方法?是否有人在網上找到了一個工作示例?每個意見都是值得歡迎的......

+0

不要將字符串從客戶端插入到這樣的數據庫中。使用[預先準備的語句](http://php.net/manual/en/pdo.prepared-statements.php)或類似的東西。 –

回答

0

我認爲,在與非Java服務器交互時使用JSON很好。您可以通過使用JSON模塊建立在一個更安全的方式在客戶端的JSON字符串:

在你.gwt.xml文件中添加

<inherits name='com.google.gwt.json.JSON'/> 

然後你就可以建立你的例子字符串如

final JSONObject jsonObject = new JSONObject(); 

jsonObject.put("done", JSONBoolean.getInstance(false)); 
jsonObject.put("description", new JSONString("Some text")); 
jsonObject.put("priority", new JSONNumber(0)); 

final String jsonString = jsonObject.toString();