我正在创建一个简单的应用并首次使用$resource
。我试图将数据保存到位于不同文件夹中的JSON对象。但是我收到以下错误:
无法加载资源:服务器响应状态为405(方法不允许)
以下是我的代码:
NewEvent.html
<form name="editEventForm">
<fieldset>
<label for="eventName">Event Name:</label>
<input id="eventName" type="text" required placeholder="Name of your event..." ng-model="event.name"> </br></br>
</fieldset>
<button type="submit" ng-disabled="editEventForm.$invalid" ng-click="saveEvent(event, editEventForm)">Save</button>
<button type="button" ng-click="cancelEdit()">Cancel</button>
</form>
EditEventController.js
eventsApp.controller('EditEventController',
function EditEventController($scope, eventData){
$scope.saveEvent = function(event, editEventForm){
if(editEventForm.$valid){
eventData.save(event);
console.log("Succeed");
//window.alert('Event ' + {{event.name}} + ' is created.');
}
}
$scope.cancelEdit = function(){
window.location = 'EventDetails.html';
}
});
EventData.js
eventsApp.factory('eventData', function($resource, $q){
var resource = $resource('data/event/:id', {id: '@id'});
return {
getEvent: function() {
var deferred = $q.defer();
resource.get({id:"1.json"},
function(event) {
deferred.resolve(event);
},
function(response) {
deferred.reject(response);
}
);
return deferred.promise;
},
save: function(event) {
var deferred = $q.defer();
event.id = 999;
resource.save(event,
function(response) {deferred.resolve(response);},
function(response) {deferred.reject(response);}
);
return deferred.promise;
}
};
});
答案 0 :(得分:0)
您无法将数据发布到静态资源文件,例如.json
扩展名
您必须在服务器上使用编程语言来操作该文件。
请求应转到服务器端脚本端点