我是BackboneJS和Jquery的新手,所以當嘗試從php數組返回JSON數據時遇到問題。BackboneJS將PHP數組返回到[object object]
這是我的代碼: 型號:
var NoteModel = Backbone.Model.extend({
initialize:function(){
console.log('Model initialize');
},
url : '../www/api/controller.php'
});
查看:
var NoteView = Backbone.View.extend({
el : '#result',
// /var note = new NoteModel();
//template : _.template($("#result-view-template").html()),
initialize:function(){
console.log('View initialize');
},
render :function(){
//var note = new NoteModel();
var note = new NoteModel();
var template = _.template($("#result-view-template").html(),{result : note});
this.$el.html(template);
return this;
}
});
路線:
Backbone.emulateHTTP = true;
Backbone.emulateJSON = true;
var NoteRouter = Backbone.Router.extend({
routes : {
"result" : "displayMessage"
},
displayMessage : function(){
var notemodel = new NoteModel();
var noteview = new NoteView();
notemodel.fetch({
success: function(){
noteview.render();
}
})
}
});
var router = new NoteRouter();
Backbone.history.start();
PHP API:
<?php
$datas = array(
'result '=> 'PHP array data'
);
$data = json_encode($datas);
echo $data;
?>
HTML視圖:
<div id="result"></div>
<script type="text/template" id="result-view-template">
<%= result %>
<p>result should be here</p>
</script>
它應該在#result DIV返回PHP數組數據上#result股利,但它不是,代替返回[對象的對象。
你有沒有在你的網絡面板看到(在Firebug或網絡工具)什麼是真正到達你的瀏覽器,而不是<%= result %>? – Jorgeblom
我做的console.log(noteview),並返回此 環R {CID: 「視圖2」,$ EL:n.fn.init [1],EL:#DIV結果,構造:函數,初始化:函數...} –