【问题标题】:Backbone.js save gets '404 not found' on Slim.phpBackbone.js 保存在 Slim.php 上得到 '404 not found'
【发布时间】:2012-09-04 11:17:54
【问题描述】:

我创建了一个 Backbone.js 模型并希望将一个实例保存到我的 MySql 数据库中。

var Website = Backbone.Model.extend({
    defaults: {   

                "title":"default title"
            },
    urlRoot : './websites'
});


var website = new Website();
website.save();​

我正在使用 Slim.php 为我的数据库创建一个 Restful API。这里是 website\index.php 的开头:

<?php

require 'Slim/Slim.php';

$app = new Slim();

$app->get('/websites', 'getWebsites');
$app->get('/websites/:id',  'getWebsite');
$app->post('/websites', 'addWebsite');
$app->put('/websites/:id', 'updateWebsite');
$app->delete('/websites/:id',   'deleteWebsite');

$app->run();

我的 save() 触发了一个“永久移动”的 POST:

请求 网址:localhost/SAMPLE-CODES/backbone.js-mysql-reading-json/websites 请求方法:POST 状态码:301 永久移动

然后我看到发送了第二个 http 请求:

请求 网址:localhost/SAMPLE-CODES/backbone.js-mysql-reading-json/websites/ 请求方法:GET 状态码:404 Not Found

我的问题是:为什么这个请求没有触发对“addWebsite”函数的调用? 我看到第二个 http 请求是一个 GET,它应该是一个 POST,但即使这样,也有一个路由......

我有一个文件夹 /websites/

我还根据Slim routing documentation 设置了 .htaccess 和 http.conf:

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

http.conf

<VirtualHost *:80>
    <Directory "c:/xampp/htdocs/SAMPLE-CODES/backbone.js-mysql-reading-json/websites/">
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

编辑

对 model.save() 的调用会触发一个 http POST 到

localhost/SAMPLE-CODES/backbone-mysql-reading-json/网站

.

这会得到一个“301 Moved Permanently”,响应头显示:

localhost/SAMPLE-CODES/backbone-mysql-reading-json/websites/

.

我认为这是由于 Slim 推荐的 .htaccess 设置。

问题:是否可以将此“301 永久移动”,还是我在这里已经遇到问题了?

然后我看到第二个 http GET 到

localhost/SAMPLE-CODES/backbone-mysql-reading-json/websites/


更新

我仍然收到 301

localhost/SAMPLE-CODES/backbone-mysql-reading-json/网站

,然后是 GET 到

localhost/SAMPLE-CODES/backbone-mysql-reading-json/websites/

第二个请求在浏览器上运行并返回

[{"id":"1","title":"titre site 1"},{"id":"2","title":"titre site 2"}]

所以这将是所有记录的响应,但我想保存 1 条记录。 看来重定向301是错误的。

我感觉这是由于 Slim 没有找到匹配的路线 (Slim_Exception_RequestSlash would trigger a 301)。

但是为什么我的 Slim 脚本找不到路由呢?

请求

localhost/SAMPLE-CODES/backbone-mysql-reading-json/网站

匹配:

$app->post('/websites', function() {

【问题讨论】:

    标签: .htaccess backbone.js slim


    【解决方案1】:

    我也使用 SlimPHP,并且有一个和你一样的设置。

    你用的是什么PHP?如果您使用的版本 >= 5.3,那么您定义路线的方式将不正确。对于较新的 PHP,试试这个:

    $app->get('/websites', function () {
        // your code to execute
    });
    

    在我看来,您设置的其他所有内容都很正常。

    【讨论】:

    • 感谢您的提示。我确实有 PHP 5.3.5 并按照您的建议更改了 index.php,但我仍然得到“404 Not Found”。
    • 我还是不知所措。 '301 Moved permently' 是正常的,还是我已经有问题了?
    • 奇怪的是你得到一个 301。在你的结构中,http://localhost/SAMPLE-CODES/backbone.js-mysql-reading-json/websites/ 你的 index.php 文件在哪里,你的 .htaccess 文件在哪里?
    • index.php 和 .htaccess 都在 c:/xampp/htdocs/SAMPLE-CODES/backbone.js-mysql-reading-json/websites/
    • 这可能是问题所在。我的结构 http://localhost/application/someFolder 和我的 index.php 和 .htaccess 在应用程序根级别。也就是说,http://localhost/application/index.php & http://localhost/application/.htaccess 然后当我想访问我的模型网址时,我只需执行urlRoot: 'websites'。路由从您的索引和 .htaccess 所在的位置开始,然后按照您的定义,将查找子文件夹 /websites。试试看,如果它有效,请回复我。如果是这样,让我们​​将此评论转换为正式答案。
    【解决方案2】:

    由于 orangewarp 和这个问题,问题得以解决:

    Why do I get a 301 redirect to folder name with slash?

    解决方案是在 .htaccess 中添加DirectorySlash Off

    我更新的 .htaccess(应用调用 Slim.php):

    RewriteEngine On
    DirectorySlash Off
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [QSA,L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2019-06-16
      • 2023-01-12
      • 1970-01-01
      相关资源
      最近更新 更多