2014-09-23 48 views
1

我正在嘗試簡單的把手。 最初我已經嘗試使用靜態JavaScript數組對象的handlebars數據,它工作正常。 所以我嘗試使用JavaScript Array對象作爲JSON字符串從Java RestURL(使句柄數據動態)和編譯句柄。在那裏我通過AJAX調用從RestURL獲取數據,甚至模板也是從handlebars構建的。真正的問題是調用模板函數時,它不會生成帶模板的響應數據。 以下是HTML代碼:Handlebars.js不能使用AJAX JSON響應

<html> 
 
<head> 
 
<title>Handlebars.js Demo</title> 
 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"> </script> 
 
<script src="js/handlebars.js"> </script> 
 
<script id="some-template" type="text/x-handlebars-template"> 
 
<table> 
 
<thead> 
 
<th>id</th> 
 
<th>username</th> 
 
<th>password</th>  
 
</thead> 
 
<tbody> 
 
{{#Users}} 
 
<tr> 
 
<td>{{id}}</td> 
 
<td>{{username}}</td> 
 
<td>{{password}}</td>   
 
</tr> 
 
{{/Users}} 
 
</tbody> 
 
</table> 
 
</script> 
 
</head> 
 
<body> 
 
<div id="content"></div> 
 
<script> 
 
$(document).ready(function() 
 
{ 
 
$.ajax({ 
 
url:"http://localhost:8082/Backbone/rest/test/test1", 
 
method:'get', 
 
success:function (results) 
 
{ 
 
var source = $("#some-template").html(); 
 
var template = Handlebars.compile(source); 
 
$("#content").html(template(result)); 
 
} 
 
}) \t 
 
}); 
 
</script> 
 
</html>

該休息的java代碼:

@Path("/test") 
public class Common 
{ 
@GET 
@Path("/test1") 
public Response getMsg() 
{ 
Users user=new Users(1,"harish","ram"); 
String output=toJson(user); 
return Response.status(200).entity(output).build(); 
} 
public String toJson(Object object) 
{ 
Gson gson=new Gson(); 
String str=gson.toJson(object); 
return str; 
} 
} 

的用戶等級:

public class Users 
{ 
    private int id; 
    private String username; 
    private String password; 
    public Users(int id,String username, String password){ 
    this.id=id; 
    this.username = username; 
    this.password = password; 
    } 
    public int getId() { 
    return id; 
    } 
    public void setId(int id){ 
    this.id = id; 
    } 
    public String getUsername(){ 
    return username; 
    } 
    public void setUsername(String username){ 
    this.username = username; 
    } 
    public String getPassword(){ 
    return password; 
    } 
    public void setPassword(String password){ 
    this.password = password; 
    } 
} 

請幫我解決這個問題。

+0

請提供後端的樣本響應解決了這個問題,所以我們可以更好地瞭解什麼正在在客戶端完成。 – thriqon 2014-09-23 06:44:43

+0

你確定它沒有跨域問題嗎?確保調用成功回調函數。 – Fancyoung 2014-09-23 06:47:39

+0

這是RestURL {「id」:1,「username」:「harish」,「password」:「ram」}的響應。 – 2014-09-23 07:10:59

回答

0

我已經在RestURL改變如下

@GET 
@Path("/test1") 
@Produces(MediaType.APPLICATION_JSON) 
public JSONObject getMsg() throws JSONException 
{ 
    String output ="{ users: [{username: \"alan\", firstName: \"Alan\", lastName: \"Johnson\", email: \"[email protected]\" },{username: \"allison\", firstName: \"Allison\", lastName: \"House\", email: \"[email protected]\" },{username: \"ryan\", firstName: \"Ryan\", lastName: \"Carson\", email: \"[email protected]\" }]};"; 
    JSONObject obj = new JSONObject(); 
    obj.put("id", 3); 
    obj.put("userName", 1); 
    obj.put("password", 2); 
    return obj;   
}