我是一名有用的助手,可以为您翻译文本。
我有一个Angular应用程序作为前端在localhost:9000上,后端使用Sails框架在localhost:1337上。
在Angular应用程序中,我这样查询服务器:
行为有点奇怪。如果我从Firefox进入angular应用程序并尝试触发saveFood函数:
跨域请求被阻止:同源策略不允许读取远程资源localhost:1337/food。这可以通过将资源移动到相同的域或启用CORS来解决。
在Chrome中有点不同。有时我会得到CORS错误,有时不会,有时甚至服务器上的模型也会更新值...但页面显示“无法POST /”。
现在在我的sails应用程序中,我已经像这样启用了CORS: https://github.com/tarlepp/angular-sailsjs-boilerplate/blob/master/backend/config/cors.js#L71 编辑:当我检查请求的标头时,我看到2个POST,一个在localhost:1337/food上,其中包含Access-Control-Allow-Origin:localhost:9000,另一个在localhost:9000/#/food上,没有并且获取404
这是chrome控制台的一个例子输出:
有一次它没有发布,另一次却发布了。但我总是在页面上看到“无法发布/”的提示。 如果您想要重现它,所有的代码和步骤都在这里: http://okamuuu.hatenablog.com/entry/2014/04/10/135240
我有一个Angular应用程序作为前端在localhost:9000上,后端使用Sails框架在localhost:1337上。
在Angular应用程序中,我这样查询服务器:
angular.module('webApp')
.controller('FoodCtrl', function($scope, $resource) {
var Food = $resource('http://localhost:1337/food', {
id: '@id'
}, {
update: {
method: 'PUT'
}
});
$scope.food = new Food();
$scope.foodList = Food.query();
$scope.saveFood = function() {
var method = $scope.food.id ? '$update' : '$save';
$scope.food[method]({}, function() {
$scope.food = new Food();
$scope.foodList = Food.query();
console.log("Saved!!");
})
};
行为有点奇怪。如果我从Firefox进入angular应用程序并尝试触发saveFood函数:
跨域请求被阻止:同源策略不允许读取远程资源localhost:1337/food。这可以通过将资源移动到相同的域或启用CORS来解决。
在Chrome中有点不同。有时我会得到CORS错误,有时不会,有时甚至服务器上的模型也会更新值...但页面显示“无法POST /”。
现在在我的sails应用程序中,我已经像这样启用了CORS: https://github.com/tarlepp/angular-sailsjs-boilerplate/blob/master/backend/config/cors.js#L71 编辑:当我检查请求的标头时,我看到2个POST,一个在localhost:1337/food上,其中包含Access-Control-Allow-Origin:localhost:9000,另一个在localhost:9000/#/food上,没有并且获取404
这是chrome控制台的一个例子输出:
2Navigated to http://localhost:9000/
(index):1 POST http://localhost:9000/ 404 (Not Found)
2Navigated to http://localhost:9000/
(index):1 POST http://localhost:9000/ 404 (Not Found)
2Navigated to http://localhost:9000/
food.js:33 Saved!!
(index):1 POST http://localhost:9000/ 404 (Not Found)
Navigated to http://localhost:9000/
有一次它没有发布,另一次却发布了。但我总是在页面上看到“无法发布/”的提示。 如果您想要重现它,所有的代码和步骤都在这里: http://okamuuu.hatenablog.com/entry/2014/04/10/135240