我是MEAN应用程序的新手。这里有一个基于REST的示例应用程序,使用节点 - restful库,我可以执行操作(获取,保存,删除),除了'put'。然而,'put'操作适用于其他客户端(高级REST,邮递员)但不适用于角客户端。
猫鼬模型
var restful = require('node-restful'); var mongoose = restful.mongoose; // Schema var productSchema = new mongoose.Schema({ name: String, college: String, age: Number }); // Return model module.exports = restful.model('Products', productSchema);
节点快速代码
var express = require('express'); var methodOverride = require('method-override'); var mongoose = require('mongoose'); var bodyParser = require('body-parser'); var cors =require('cors'); // MongoDB mongoose.connect('mongodb://localhost/rest_test'); var autoIncrement = require('mongoose-auto-increment'); // Express var app = express(); app.use(methodOverride('_method')); app.use(cors()); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); // Routes app.use('/api', require('./routes/api')); // Start server app.listen(4000); console.log('API is running on port 4000');
更新数据的角度函数
$scope.updateData = function (userID) { $scope.actionData = { "name": $scope.name, "college": $scope.college, "age": $scope.age } RoleEditF.updateUserData({ userId: userID }, $scope.actionData).then(function (response) { $scope.userData = response; console.log($scope.userData) $scope.getData(); }).catch(function (response) { $scope.error = "Unable to get Files (code: " + response.status + "). Please try later."; }); }
angular.module('myapp') .factory('RoleEditF', function (updateS) { return { updateUserData: function (parm, data, callback) { var cb = callback || angular.noop; return updateS.save(parm, data, function (res) { return cb(res); }, function (err) { return cb(err); }.bind(this)).$promise; } } })
工厂调用API
angular.module('myapp') .factory('updateS',function($resource) { return $resource('http://localhost:4000/api/products/:userId', { userId: '@userId' }, { update: { method: 'PUT' } }, { stripTrailingSlashes: false }); });
我在浏览器上遇到以下错误
"NetworkError: 404 Not Found - http://localhost:4000/api/products/57161e0fe4fbae354407baa3"
答案 0 :(得分:2)
必须在
中“更新” 'update': {
method: 'PUT'
}
在$ resource()工厂内
这里的文档 https://docs.angularjs.org/api/ngResource/service/$resource
创建自定义'PUT'请求