AngularJS - PUT方法不工作(404错误) [英] AngularJS - PUT method not working (404 error)
问题描述
我是新来AngularJS和我有麻烦通过REST更新的对象。我使用的是PHP / MySQL的后端(超薄框架)。结果
我能够检索(GET),创建(POST)的新对象,但不能编辑(PUT)之一。这里的code:
I'm new to AngularJS and I have trouble to update an object via REST. I'm using a PHP/Mysql Backend (Slim Framework).
I'm able to retrieve (GET), create (POST) a new object but not to edit (PUT) one. Here's the code:
我的方式:
<form name="actionForm" novalidate ng-submit="submitAction();">
Name: <input type="text" ng-model="action.name" name="name" required>
<input type="submit">
</form>
我的服务:
var AppServices = angular.module('AppServices', ['ngResource'])
AppServices.factory('appFactory', function($resource) {
return $resource('/api/main/actions/:actionid', {}, {
'update': { method: 'PUT'},
});
});
app.js
app.js
var app = angular.module('app', ['AppServices'])
app.config(function($routeProvider) {
$routeProvider.when('/main/actions', {
templateUrl: 'partials/main.html',
controller: 'ActionListCtrl'
});
$routeProvider.when('/main/actions/:actionid', {
templateUrl: 'partials/main.html',
controller: 'ActionDetailCtrl'
});
$routeProvider.otherwise({redirectTo: '/main/actions'});
});
controllers.js:
controllers.js:
function ActionDetailCtrl($scope, $routeParams, appFactory, $location) {
$scope.action = appFactory.get({actionid: $routeParams.actionid});
$scope.addAction = function() {
$location.path("/main/actions/new");
}
$scope.submitAction = function() {
// UPDATE CASE
if ($scope.action.actionid > 0) {
$scope.action = appFactory.update($scope.action);
alert('Action "' + $scope.action.title + '" updated');
} else {
// CREATE CASE
$scope.action = appFactory.save($scope.action);
alert('Action "' + $scope.action.title + '" created');
}
$location.path("/main/actions");
}
}
在修身,在API / index.php文件,我已经得到了这些路线和定义的功能:
In Slim, in api/index.php, I've got these routes and functions defined:
$app->get('/main/actions', 'getActions');
$app->get('/main/actions/:actionid', 'getAction');
$app->post('/main/actions', 'addAction');
$app->put('/main/actions/:actionid', 'updateAction');
当我创建了一个新的行动,一切工作正常。但是,当我尝试编辑现有的,我有这个错误:
When I create a new "action", everything is working as expected. But when I try to edit an existing one, I've got this error:
HTTP://project.local/api/main/actions 404未找到
动作不更新(虽然会显示警告信息行动XXX更新)
The action is not updated (although the alert message "Action xxx updated" is displayed)
时它与我的routeProvider设置的问题吗?我猜PUT URL错过了ID在年底...
Is it an issue with my routeProvider setting ? I guess the PUT url misses the id at the end...
我precise,如果我试图以模拟<一个PUT请求href=\"https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm\"相对=nofollow>邮递员 - 铬 - 扩展例如,一切运作良好(PUT的 HTTP://project.local/api/main/actions/3 返回预期的数据)
I precise that if I try to simulate the PUT request with POSTMan-Chrome-Extension for example, everything is working well (PUT http://project.local/api/main/actions/3 returns the expected data)
推荐答案
更仔细地看完后,这个问题确实是,你不发送ID在URL中,当你做了更新的要求。此外,您拨打更新的方式/保存是相当有什么打算不与资源。你已经创建了一个资源后,你打电话时,保存/更新方法的实例,而不是作为一个工厂函数。您还可以绑定实例在URL中则params的属性,所以您不必指定它们每次调用:
After reading more carefully, the problem is indeed that you don't send the id in the url when you do the update request. Furthermore, the way you call update / save is not quite what is intended with resource. After you have created a resource, you call the save / update methods on the instance, not as a factory function. You can also bind properties of the instance to the params in the url, so you don't have to specify them on each call:
AppServices.factory('appFactory', function($resource) {
return $resource('/api/main/actions/:actionid', {actionid : '@actionid'}, {
'update': { method: 'PUT'},
});
资源的第二个参数告诉角度的属性,应该用于填充URL参数。
所以GET操作保持不变:
The second parameter of resource tells angular which properties it should use to populate the url parameters. so the get operation stays the same:
$scope.action = appFactory.get({actionid: $routeParams.actionid});
和现在的更新是:
$scope.submitAction = function() {
// UPDATE CASE
if ($scope.action.actionid > 0) {
$scope.action.$update()
} else {
$scope.action.$save();
}
}
由于您使用角地窖作为基础,我也许应该检查,看看我是否能改善这种...
Since you used angular-cellar as a basis, I should probably check and see if I can improve this ...
这篇关于AngularJS - PUT方法不工作(404错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!