PHP + Backbone.js 保存回调不断引发错误
首先,我想说,我首先是一名平面设计师,所以如果我错过了一些非常愚蠢的东西,我真的很抱歉,但我自己无法解决问题:/。
我在使用backbone.js 和PHP 保存模型时遇到问题。我正在使用 Restler 来处理 PHP 调用。
我正在为一个我们将举办会议的网站做管理员。当我将发言人添加到会议时,我在 pl_speakers 表中创建一个条目,并且我想检索其 ID 以便将其添加到相应的会议条目。
我的 PHP 代码如下所示:
function post($id = NULL, $request_data = NULL) {
$sql = "INSERT INTO pl_speakers(id, firstName, name, job, site, mail, video, cv) VALUES (
'',
'".$request_data["firstName"]."',
'".$request_data["name"]."',
'".$request_data["job"]."',
'".$request_data["site"]."',
'".$request_data["mail"]."',
'".$request_data["video"]."',
'".$request_data["cv"]."'
)";
$req = $this->db->query($sql);
$response = array("id" => $this->db->lastInsertId());
return json_encode($response);
}
我的 javascript 如下所示
onAddClick: function(){
var speaker = new speakerModel;
this.speakers.add(speaker);
},
onAddSpeaker: function(speakerModel){
speakerModel.save(speakerModel, { success:$.proxy(this.onSpeakerSaved, this), error: $.proxy(this.onSpeakerNotSaved, this)});
},
onSpeakerSaved: function(speakerModel, response){
console.log(response)
},
当我查看 Chrome 的网络面板时,php 脚本发送的响应是“{\”id\”:\“128\”}”,但响应已登录“ onSpeakerSaved”没有出现,而是出现此错误:
Uncaught TypeError: Cannot use 'in' operator to search for 'id' in {"id":"128"}
f.extend.setbackbone-min.js:9
f.extend.save.b.successbackbone-min.js:12
f.extend._Deferred.e.resolveWithjquery-min.js:2
wjquery-min.js:4
f.support.ajax.f.ajaxTransport.send.d
无论 php 函数返回什么(即使它是整个模型),我都会收到此错误,但如果它不返回任何内容,则错误消失。
我搜索了有关该主题的先前问题,似乎响应必须是一个带有“id”属性的 json 对象,它就在这里,所以我完全陷入困境,无法弄清楚我错在哪里: /。
再说一遍,如果我错过了一件愚蠢的事情,我真的很抱歉,但我真的很感激能帮上忙:)
编辑: 这是整个 php 类,如果它有任何帮助的话,但我觉得它更多的是在 javascript 方面以及 Backbone 如何处理响应。我还添加了 onAddClick JS 方法。
class Speakers {
public $db;
function __construct() {
$this->db = new PDO("mysql:dbname=".DB_NAME.";host=".DB_SERVER, DB_USER, DB_PWD);
}
function get($id=null) {
return is_null($id) ? $this->getAll() : $this->getSpeaker($id);
}
function getAll() {
$sql = "SELECT * FROM pl_speakers";
return $this->getJSON($sql);
}
function getSpeaker($id) {
$sql = "SELECT * FROM pl_speakers WHERE id='".$id."'";
return $this->getJSON($sql);
}
function getJSON($sql) {
$req = $this->db->query($sql);
$data = array();
while($row = $req->fetchObject())
{
$data[] = $row;
}
return json_encode($data);
}
function post($id = NULL, $request_data = NULL) {
$sql = "INSERT INTO pl_speakers(id, firstName, name, job, site, mail, video, cv) VALUES (
'',
'".$request_data["firstName"]."',
'".$request_data["name"]."',
'".$request_data["job"]."',
'".$request_data["site"]."',
'".$request_data["mail"]."',
'".$request_data["video"]."',
'".$request_data["cv"]."'
)";
$req = $this->db->query($sql);
$response = array("id" => $this->db->lastInsertId());
return $this->getSpeaker($this->db->lastInsertId());
}
function put($id = NULL, $request_data = NULL) {
$sql = "UPDATE pl_speakers
SET firstName = '".$request_data["firstName"]."',
name = '".$request_data["name"]."',
job = '".$request_data["job"]."',
site = '".$request_data["site"]."',
mail = '".$request_data["mail"]."',
video = '".$request_data["video"]."',
cv = '".$request_data["cv"]."'
WHERE id = '".$id."'";
$req = $this->db->query($sql);
}
function delete($id = NULL) {
$sql = "DELETE FROM pl_speakers WHERE id='".$id."'";
$req = $this->db->query($sql);
}
First, I'd like to say that I'm a graphic designer at first, so I'm really sorry if I'm missing something really stupid but I can't figure out the problem by myself :/.
I'm running into a problem when saving a model with backbone.js and PHP. I'm using Restler to handle the PHP calls.
I'm working on an admin for a website that we'll present conferences. When I add a speaker to a conference, I create an entry in the pl_speakers table and I want to retrieve its id in order to add it to the corresponding conference entry.
My PHP code looks like this :
function post($id = NULL, $request_data = NULL) {
$sql = "INSERT INTO pl_speakers(id, firstName, name, job, site, mail, video, cv) VALUES (
'',
'".$request_data["firstName"]."',
'".$request_data["name"]."',
'".$request_data["job"]."',
'".$request_data["site"]."',
'".$request_data["mail"]."',
'".$request_data["video"]."',
'".$request_data["cv"]."'
)";
$req = $this->db->query($sql);
$response = array("id" => $this->db->lastInsertId());
return json_encode($response);
}
and my javascript looks like that
onAddClick: function(){
var speaker = new speakerModel;
this.speakers.add(speaker);
},
onAddSpeaker: function(speakerModel){
speakerModel.save(speakerModel, { success:$.proxy(this.onSpeakerSaved, this), error: $.proxy(this.onSpeakerNotSaved, this)});
},
onSpeakerSaved: function(speakerModel, response){
console.log(response)
},
When I look in Chrome's Network panel, the response send by the php script is "{\"id\":\"128\"}" but the response logged in "onSpeakerSaved" doesn't appear and instead I have this error :
Uncaught TypeError: Cannot use 'in' operator to search for 'id' in {"id":"128"}
f.extend.setbackbone-min.js:9
f.extend.save.b.successbackbone-min.js:12
f.extend._Deferred.e.resolveWithjquery-min.js:2
wjquery-min.js:4
f.support.ajax.f.ajaxTransport.send.d
I get this error whatever the php function returns (even if it's the whole model) but if it doesn't return anything then the error disappears.
I've searched the previous questions on the subject and it seems the response must be a json object with an "id" property, which it is here, so I'm totally stuck and can't figure out where I'm wrong :/.
Again, if I'm missing a stupid thing, I'm really sorry but I would really appreciate a hand on this one :)
EDIT :
Here's the whole php class if it can be of any help but I feel like it's more on the javascript side and how the response is handled by Backbone. I also added the onAddClick JS method.
class Speakers {
public $db;
function __construct() {
$this->db = new PDO("mysql:dbname=".DB_NAME.";host=".DB_SERVER, DB_USER, DB_PWD);
}
function get($id=null) {
return is_null($id) ? $this->getAll() : $this->getSpeaker($id);
}
function getAll() {
$sql = "SELECT * FROM pl_speakers";
return $this->getJSON($sql);
}
function getSpeaker($id) {
$sql = "SELECT * FROM pl_speakers WHERE id='".$id."'";
return $this->getJSON($sql);
}
function getJSON($sql) {
$req = $this->db->query($sql);
$data = array();
while($row = $req->fetchObject())
{
$data[] = $row;
}
return json_encode($data);
}
function post($id = NULL, $request_data = NULL) {
$sql = "INSERT INTO pl_speakers(id, firstName, name, job, site, mail, video, cv) VALUES (
'',
'".$request_data["firstName"]."',
'".$request_data["name"]."',
'".$request_data["job"]."',
'".$request_data["site"]."',
'".$request_data["mail"]."',
'".$request_data["video"]."',
'".$request_data["cv"]."'
)";
$req = $this->db->query($sql);
$response = array("id" => $this->db->lastInsertId());
return $this->getSpeaker($this->db->lastInsertId());
}
function put($id = NULL, $request_data = NULL) {
$sql = "UPDATE pl_speakers
SET firstName = '".$request_data["firstName"]."',
name = '".$request_data["name"]."',
job = '".$request_data["job"]."',
site = '".$request_data["site"]."',
mail = '".$request_data["mail"]."',
video = '".$request_data["video"]."',
cv = '".$request_data["cv"]."'
WHERE id = '".$id."'";
$req = $this->db->query($sql);
}
function delete($id = NULL) {
$sql = "DELETE FROM pl_speakers WHERE id='".$id."'";
$req = $this->db->query($sql);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Restler 负责根据客户端请求的内容以及您希望 API 支持的格式将您的方法返回的 PHP 数据转换为 JSON/XML/YAML/PLIST/AMF
因此您不应该尝试自己进行 JSON 编码需要返回的是一个数组。
这是您的课程的修改版本
Restler takes care of converting PHP data returned by your methods in to JSON/XML/YAML/PLIST/AMF based on what client is requesting and what formats you want your API to support
So you should not try doing the JSON encode yourself, all you need to return is an array.
Here is the modified version of your class