我正在尝试将 Emberjs 与 PHP 一起用作后端。
这是我的app.js
,
App = Ember.Application.create({});
App.IndexRoute = Ember.Route.extend({
renderTemplate : function(controller) {
this.render('MyApp', {
controller : controller
});
},
model : function() {
return App.MyTemplateModel.find();
}
});
App.IndexController = Ember.ArrayController.extend({
filteredContent : Ember.computed.oneWay("content"),
last : function() {
var lastName = App.controller.get('selectedProgrammer.last_name');
var filtered = this.get('content').filterProperty('last_name', lastName);
this.set("filteredContent", filtered);
},
refresh : function() {
var refresh = this.get('content');
this.set("filteredContent", refresh);
}
});
App.MyTemplateModel = Ember.Model.extend({
id : Ember.attr(),
last_name : Ember.attr(),
first_name : Ember.attr(),
suffix : Ember.attr(),
expiration : Ember.attr()
});
App.controller = Ember.Object.create({
selectedProgrammer : null,
content : [Ember.Object.create({
last_name : "Solow",
id : 1
}), Ember.Object.create({
last_name : "Arbogast",
id : 2
}), Ember.Object.create({
last_name : "Dorfman",
id : 3
}), Ember.Object.create({
last_name : "Eliason",
id : 4
})]
});
App.MyTemplateModel.url = "user.php";
App.MyTemplateModel.adapter = Ember.RESTAdapter.create({
ajaxSettings: function(url, get) {
return {
url: url,
type: get
};
}
});
var existing = App.MyTemplateModel.find();
App.MyTemplateModel.camelizeKeys = true;
和我的 PHP 代码,
<?php
$con = mysql_connect("localhost","root","school");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("codeigniter", $con);
$id = $_GET['id'];
$query = "SELECT * FROM `user`";
$comments = mysql_query($query);
while($row = mysql_fetch_array($comments, MYSQL_ASSOC))
{
$name = $row['first_name'];
echo json_encode($name);
}
mysql_close($con);
?>
但我在控制台上得到这个,
GET http://ember.local/user.php.json 404 (Not Found)
我可以看到它正在将 .json 添加到 php 文件中,但为什么呢?此外,我如何解决它或如何在 Ember 中实现我自己的 Ajax 调用?此外,我在代码中使用了 Ember 模型。