php 处理来自backbone.js 的put 请求

发布于 2024-11-14 05:36:24 字数 84 浏览 1 评论 0原文

当backbone.js将模型保存到服务器时,它会发送一个PUT请求。我如何用 php 处理这些?如何获取随 put 请求发送的内容并将其存储在数据库中?

When backbone.js saves a model to the server, it sends a PUT request. How do I handle these with php? How do I take the contents that are sent with the put request, and store them in a database?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

氛圍 2024-11-21 05:36:24

这是另一个例子:

$values = json_decode(file_get_contents('php://input'), true);

  • 这将产生一个数组(json_decode() 的第二个参数)$values,其中将包含您的密钥 =>接收到的 json 数据的值对。

Here is another example:

$values = json_decode(file_get_contents('php://input'), true);

  • This would result in an Array (second parameter of json_decode()) $values which would contain your key => value pairs of the received json data.
旧故 2024-11-21 05:36:24

请参阅 php 文档示例 http://php.net/manual/en/features 来自 php.net 的.file-upload.put-method.php

<?php
/* PUT data comes in on the stdin stream */
$putdata = fopen("php://input", "r");

/* Open a file for writing */
$fp = fopen("myputfile.ext", "w");

/* Read the data 1 KB at a time
   and write to the file */
while ($data = fread($putdata, 1024))
  fwrite($fp, $data);

/* Close the streams */
fclose($fp);
fclose($putdata);
?>

当您想要将数据存储到数据库时,您可以省略 fwrite 部分。

see the php docs for an example http://php.net/manual/en/features.file-upload.put-method.php

from php.net:

<?php
/* PUT data comes in on the stdin stream */
$putdata = fopen("php://input", "r");

/* Open a file for writing */
$fp = fopen("myputfile.ext", "w");

/* Read the data 1 KB at a time
   and write to the file */
while ($data = fread($putdata, 1024))
  fwrite($fp, $data);

/* Close the streams */
fclose($fp);
fclose($putdata);
?>

you can leave the fwrite part out when you want to store the data to a DB.

琉璃繁缕 2024-11-21 05:36:24
Backbone.emulateHTTP = true;

如果您想要使用不支持 Backbones 默认 REST/HTTP 方法的旧版 Web 服务器,您可以选择打开 Backbone.emulateHTTP。设置此选项将使用 HTTP POST 伪造 PUT 和 DELETE 请求,并将它们传递到 _method 参数下。设置此选项还将使用 true 方法设置 X-HTTP-Method-Override 标头。

之后,在模型中实现您自己的 sync 函数:http://documentcloud. github.com/backbone/#Sync

Backbone.emulateHTTP = true;

If you want to work with a legacy web server that doesn't support Backbones's default REST/HTTP approach, you may choose to turn on Backbone.emulateHTTP. Setting this option will fake PUT and DELETE requests with a HTTP POST, and pass them under the _method parameter. Setting this option will also set an X-HTTP-Method-Override header with the true method.

After that implement your own sync function in your model: http://documentcloud.github.com/backbone/#Sync

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文