$ http到Rails ApplicationController对角度1.1+构建给出"HTTP错误406不可接受"

rub*_*yno 5 ruby-on-rails http-headers angularjs

如果您正在调用这样的Rails服务:

 $http.get(url).success(successFn)
               .error(
                      function(data, status, headers, config) {
                               console.log('Failed');
                      }
                     );
Run Code Online (Sandbox Code Playgroud)

你的Rails ApplicationController响应如下:

   def your_service

      # do some stuff

      respond_to do |format|
         format.js { render 'shared/common_js' }
      end
   end
Run Code Online (Sandbox Code Playgroud)

使用角度1.0.x一切顺利进行,您的服务回答OK 200.

然后我尝试对不稳定的1.1.x版本使用相同的代码,并且停止使用HTTP错误406不可接受.

rub*_*yno 18

我花了几个小时调查这个,希望这会节省别人的时间.

在调试会话之后,最后我发现差异在请求标头中:

1.0.3 (at this time):  {X-Requested-With: "XMLHttpRequest", Accept: "application/json, text/plain, */*", X-XSRF-TOKEN: undefined} 

1.1.1 (at this time):  {Accept: "application/json, text/plain, */*", X-XSRF-TOKEN: undefined} 
Run Code Online (Sandbox Code Playgroud)

所以我用Google搜索"X-Requested-With:XMLHttpRequest已删除",我终于发现了这个提交:

($ http):从标题默认值中删除'X-Requested-With'https ://github.com/angular/angular.js/commit/3a75b1124d062f64093a90b26630938558909e8d

(这种删除是一些讨论的结果,基本上是为了让CORS请求更顺畅)

通过这种删除,Rails无法找到通过该服务的方式,即:

format.js { render 'shared/common_js' }  
Run Code Online (Sandbox Code Playgroud)

不再触发(实际上是format.html)!

可能的解决方法是:

    $http( {method: 'GET', url: url , headers: {'X-Requested-With': 'XMLHttpRequest', 'Accept': 'application/json, text/plain, */*'}})
           .success(successFn)
           .error(
                           function(data, status, headers, config) {
                               console.log('Fail');
                             }
                         );
Run Code Online (Sandbox Code Playgroud)

否则,如提交中所述,您可以恢复丢失的标头,如下所示:

myAppModule.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
}]);
Run Code Online (Sandbox Code Playgroud)

希望这有用,欢呼.