Angular JS Fails After Upgrade from 1.1.0 to 1.1.1

守給你的承諾、 提交于 2019-12-03 16:55:56

It's not entirely true that you have to add .json - at least not from my experience when I have switched from 1.0.x to 1.1.x.

In order to make Rails app return json without specyfing extension in URL for Angular 1.1.x you need to make angular add additional header to http request:

myModule.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
}]);

Without that Rails by default renders whole HTML instead of returning JSON.

Github issue reflecting that

The answer was in the URL file extension. Starting with Angular version 1.1.1, it is necessary to include '.json' in the resource.

 app = angular.module("Resume", ["ngResource"])

 app.factory "Entry", ["$resource", ($resource) ->
-  $resource("/entries")
+  $resource("/entries.json", {}, {}, {})
 ]

 app.factory "Done", ["$resource", ($resource) ->
-  $resource("/dones", {}, {update: {method: "PUT"}})
+  $resource("/dones.json", {}, {update: {method: "PUT"}}, {})
 ]

 app.factory "Resource", ["$resource", ($resource) ->
-  $resource("/resources", {}, {})
+  $resource("/resources.json", {}, {}, {})
 ]

Can someone clarify is this is because of my Rails 4 back-end or if it is a new Angular requirement?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!