本文介绍了设置为Backbone.js的RESTful服务与Apache和窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个RESTful Web服务在我的本地主机的Apache作为后端为我的骨干应用程序。我曾尝试:

I'm trying to set up a RESTful web service on my apache localhost to serve as the back-end for my backbone app. I have tried:


  1. 在日志中设置WebDAV,但得到以下错误消息

  1. Setting up WebDAV, but get the following error messages in the logs

[周四2月23日21点46分17秒2012] [错误] [客户端127.0.0.1]无法提出了新的内容,/集群/ 19。 [403,#0],引用者:
[周四2月23日21点46分十七秒2012] [错误] [客户端127.0.0.1]在打开一个资源时发生错误。 [500,#0],引用者:

[Thu Feb 23 21:46:17 2012] [error] [client 127.0.0.1] Unable to PUT new contents for /clusters/19. [403, #0], referer: http://ideas.localhost/[Thu Feb 23 21:46:17 2012] [error] [client 127.0.0.1] An error occurred while opening a resource. [500, #0], referer: http://ideas.localhost/

使用Backbone.emulateHTTP,这将导致不允许的405方法误差(这是我的猜测是由 X-HTTP-引起的方法-覆盖:PUT 头正常POST请求做工精细

Using Backbone.emulateHTTP, which causes a 405 method not allowed error (something I guess is caused by the X-HTTP-Method-Override: PUT header as normal POST requests are working fine

我在Windows 7上运行的Apache 2.2.21和PHP 5.3,以下是我的.htaccess文件。我还使用了SLIM框架来处理URL路由。

I'm running Apache 2.2.21 and PHP 5.3 on windows 7, and below is my .htaccess file. I'm also using the SLIM framework to handle url routing.

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

和虚拟主机配置

<VirtualHost *:80>
    DocumentRoot "G:/sites/ideas"
    Dav On // I also had security setting set to Allow all as it's just my localhost
    ServerName ideas.localhost
    ErrorLog "logs/ideas.localhost-error.log"
    CustomLog "logs/ideas.localhost-access.log" combined
    SetEnv APPLICATION_ENV development
</VirtualHost>

我一直在努力得到的东西了好半天工作,所以任何帮助极大的AP preciated。

I've been struggling to get something to work for ages, so any help greatly appreciated.

推荐答案

简直不敢相信我的问题解决了不到一个小时打开一个赏金后,但嘿嗬。

Can't believe I solved the problem less than an hour after opening a bounty, but hey ho.

问题是,斯利姆没有内置的处理由骨干网和错误消息中使用的 X-HTTP-方法,覆盖头的能力不十分生动。在增加的request.php底部以下,并在使用骨干模式emulateHTTP固定它

The problem was that Slim doesn't have a built in ability to handle the X-HTTP-Method-Override header used by backbone and the error message isn't very descriptive. Adding the following at the bottom of request.php and using emulateHTTP mode in Backbone fixed it

protected function checkForHttpMethodOverride() {
    if ( isset($this->post[self::METHOD_OVERRIDE]) ) {
        $this->method = $this->post[self::METHOD_OVERRIDE];
        unset($this->post[self::METHOD_OVERRIDE]);
        if ( $this->isPut() ) {
            $this->put = $this->post;
        }
    } else if(isset($this->headers['x-method-override'] )) {
        $this->method = $this->headers['x-method-override'];
        if ( $this->isPut() ) {
            $this->put = $this->post;
        }
    }
}

PS - 我已经创建了一个为SLIM包括这在默认情况下,如果你觉得这是一个好主意,包括在这个框架请留下评论有

PS - I've created a pull request for SLIM to include this by default, so if you think it'd be a good idea to include this in the framework please leave a comment there

这篇关于设置为Backbone.js的RESTful服务与Apache和窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 17:49