問題描述
我有一個帶有一些默認值和一個 url 的backbone.js 模型:
I have a backbone.js model with some defaults and an url:
var Box = Backbone.Model.extend({
url: "./save.php",
defaults: {
x: 0,
y: 0,
w: 1,
h: 1
}
});
然后我有一個這個模型的實例,我繼續(xù)保存它:
Then I have an instance of this model and I proceed to save it:
var box = new Box({ x:10, y:10, w:200, h:200 });
box.save();
現(xiàn)在我想使用 PHP 腳本save.php"將此模型保存到 MySQL 數(shù)據(jù)庫中,它是這樣的:
Now I want to save this model into a MySQL database using a PHP script "save.php", it goes like this:
<?php
include('connection.php');
$id = $_POST['cid'];
$x = $_POST['x'];
$y = $_POST['y'];
$w = $_POST['w'];
$h = $_POST['h'];
mysql_query("INSERT INTO boxes (id, x, y, w, h)
VALUES('$id', '$x', '$y', '$w', '$h')
") or die(mysql_error());
?>
echo "Data Inserted!";
我試過閱讀很多教程,但我無法將這個簡單的模型保存起來.為什么我的代碼不起作用?關(guān)于如何解決這個問題的任何想法?
I have tried reading many tutorials but I cannot get this simple model save to work. Why is my code not working? Any ideas on how can this be solved?
謝謝
快速解決方案
在php腳本中,從發(fā)送的JSON對象中獲取信息的正確方式如下:
In the php script, the correct way to obtain the information from the sent JSON object is as follows:
$box_data = json_decode(file_get_contents('php://input'));
$x = $box_data->{'x'};
$y = $box_data->{'y'};
$w = $box_data->{'w'};
$h = $box_data->{'h'};
并存入數(shù)據(jù)庫:
mysql_query("INSERT INTO boxes(id, x, y, w, h)
VALUES('', '$x', '$y', '$w', '$h') ")
or die(mysql_error());
這樣一來,就會在boxes"表中插入一行,其中包含主干模型Box的每個屬性的信息.本例中的服務(wù)器請求方式為POST,表boxes"中的id設(shè)置為自增.
In this way one row will be inserted in the table "boxes" with the information of each one of the attributes of the backbone model Box. The server request method in this case is POST and the id in the table "boxes" is set to auto-increment.
推薦答案
Backbone 基于 REST API:當將模型保存/更新到服務(wù)器時,Backbone 會將其序列化為 JSON 發(fā)送到請求正文中,并帶有 POST
我們的 PUT
請求.來自 Backbone.sync 文檔
Backbone is based on a REST API: when saving/updating a model to the server, Backbone will send it serialized as JSON in the request body with a POST
our PUT
request. From Backbone.sync documentation
使用默認實現(xiàn),當 Backbone.sync 發(fā)送請求時保存模型,它的屬性將被傳遞,序列化為 JSON,并在內(nèi)容類型為 application/json 的 HTTP 正文中發(fā)送.
With the default implementation, when Backbone.sync sends up a request to save a model, its attributes will be passed, serialized as JSON, and sent in the HTTP body with content-type application/json.
這意味著你必須在服務(wù)器端
This means that server-side you have to
- 確定請求的類型
- 解碼序列化的 JSON
這樣的事情應(yīng)該會讓你開始
Something like this should get you started
$request_method = strtolower($_SERVER['REQUEST_METHOD']);
$data = null;
switch ($request_method) {
case 'post':
case 'put':
$data = json_decode(file_get_contents('php://input'));
break;
}
// print_r($data);
// note that mysql_* functions are deprecated
// http://php.net/manual/en/function.mysql-query.php
// inserting with a PDO object, assuming an auto incremented id
$sql = "INSERT INTO boxes (x, y, w, h) VALUES(?, ?, ?, ?)";
$sth = $dbh->prepare($sql);
$sth->execute(array(
$data->x,
$data->y,
$data->w,
$data->h
));
$id = $dbh->lastInsertId();
查看此頁面以了解更全面的 PHP REST API 實現(xiàn) http://www.gen-x-design.com/archives/create-a-rest-api-with-php/
Check this page for a more thorough implementation of a REST API in PHP http://www.gen-x-design.com/archives/create-a-rest-api-with-php/
這篇關(guān)于將 Backbone.js 模型插入 MySQL 數(shù)據(jù)庫的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!