2012-05-21 99 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();