gpt4 book ai didi

javascript - 在 angularjs 1.0.8 中不起作用 PUT 休息服务

转载 作者:搜寻专家 更新时间:2023-10-31 23:49:00 24 4
gpt4 key购买 nike

我有一个具有 CRUD Rest 服务的 angularjs 应用程序。 Create、Read 和 Delete 方法运行良好,但 PUT 不起作用。我在 Stackoverflow 上发现,接受的答案也存在同样的问题,例如:

Angular JS: Full example of GET/POST/DELETE/PUT client for a REST/CRUD backend?

AngularJS - PUT method not working (404 error)

所以我像在这个答案中那样做,但我的更新(通过 PUT 方法)不起作用(状态代码:404 'Cannot PUT/api/adverts')。也许人们会注意到我的代码中的某些内容。一件重要的事情是我使用 angular 1.0.8

services.js (angularjs):

var motoAdsServices = angular.module('motoAdsServices', ['ngResource']);

motoAdsServices.factory('Advert', ['$resource', function($resource) {
return $resource('/api/adverts/:advertId', {}, {
update: {method:'PUT', params: {advertId: '@advertId'}}
});
}]);

editAdvert.html

  <label class="control-label">Brand</label>
<div class="controls">
<select name="brand" ng-model="editAdvert.brand" required ng-options="b.name for b in brands">
<option value=""></option>
</select>
</div>

<label class="control-label">Model</label>
<div class="controls">
<select name="model" ng-model="editAdvert.model" required ng-options="m.name for m in editAdvert.brand.models">
<option value=""></option>
</select>
</div>

controllers.js(angular.js - 我没有删除任何重要代码):

$scope.updateAdvert = function() {
var editAdvert = {
_id: $scope.editAdvert._id,
brandName: $scope.editAdvert.brand.name,
modelName: $scope.editAdvert.model.name,
year: $scope.editAdvert.year,
price: $scope.editAdvert.price,
imageUrl: $scope.editAdvert.imageUrl,
countryName: $scope.editAdvert.country.name,
regionName: $scope.editAdvert.region.name
};

// !!! I TRY THIS TO FIX !!!
$scope.advertId = $scope.editAdvert._id;

// !!! THIS NOT WORKS STILL !!!
Advert.update(editAdvert, function() {
previousAdvert = angular.copy($scope.editAdvert);
alert('Advert updated');
});
};

server.js (nodejs):

var express = require('express');
var path = require('path');
var http = require('http');
var adverts = require('./routes/adverts');

var app = express();

app.configure(function() {
app.set('port', process.env.PORT || 3000);
app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
});

app.get('/api/adverts', adverts.findAll);
app.get('/api/adverts/:id', adverts.findById);
app.post('/api/adverts', adverts.add);
app.put('/api/adverts/:id', adverts.update);
app.delete('/api/adverts/:id', adverts.remove);

http.createServer(app).listen(app.get('port'), function() {
console.log("Express server listening on port " + app.get('port'));
});

作为响应,我收到 404 和“无法放置/api/adverts”。我几乎确信这是URL末尾缺少'_id'的问题。看图片: enter image description here

请看我的代码并提出一些建议。

最佳答案

我看到这里有些不一致。首先,update 应该是实例方法。所以调用应该是这样的:$scope.editAdvert.$update(... 而不是 Advert.update($scope.editAdvert...

这里的另一件事是参数名称的不同。指示资源,资源的 id 应该是 advertId,而如图所示,对象的 ID 名为 _id

最后,您正在设置 $scope.advertId = $scope.editAdvert._id; ... 正如您所说的那样修复它。问题是,它与对象 $scope.editAdvert 无关……它只是 $scope 的另一个对象。资源不知道它。

查看有关 ngResource here 的更多详细信息

关于javascript - 在 angularjs 1.0.8 中不起作用 PUT 休息服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20018977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com