我正在尝试将数据发布到REST API,但是服务器以HTTP 405错误响应。我尝试了许多选项,例如将标头添加到POST方法,更改POST标头中的内容类型,但似乎没有任何效果。 服务器的确切响应是
{“详细信息”:“不允许使用方法\“ POST \”。“}。
HTML和JS代码
<!DOCTYPE html>
<html>
<head>
<title>
New User Story
</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('postserviceApp', []);
app.controller('postserviceCtrl', function ($scope, $http) {
$scope.jiraLink = null;
$scope.reporter = null;
$scope.assignee = null;
$scope.Reviewer = null;
$scope.summary = null;
$scope.description = null;
$scope.storyPoints = null;
$scope.postdata = function (jiralink, reporter, assignee, reviewer, summary, description, storypoints) {
var data = {
jiraLink: jiralink,
reporter: reporter,
assignee: assignee,
Reviewer: reviewer,
summary: summary,
description: description,
storyPoints: storypoints
};
//Call the services $http.post('/userStory/story', JSON.stringify(data))
$http({
method: 'POST',
url: '/userStory/story',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
},
data: data
}).then(function (response) {
if (response.data)
$scope.msg = "User story created Successfully!";
}, function (response) {
$scope.msg = "Couldn't create an User story";
$scope.statusval = response.status;
$scope.statustext = response.statusText;
$scope.headers = response.headers();
});
};
});
</script>
</head>
<body>
<div ng-app="postserviceApp" ng-controller="postserviceCtrl">
<div>
Jira Link : <input ng-model="jiralink" /><br/><br/>
Reporter : <input ng-model="reporter" /><br/><br/>
Assignee : <input ng-model="assignee" /><br/><br/>
Reviewer : <input ng-model="reviewer" /><br/><br/>
Summary : <input ng-model="summary" /><br/><br/>
Description : <input ng-model="description" /><br/><br/>
Story Points : <input ng-model="storypoints" /><br/><br/>
<input type="button" value="Create" ng-click="postdata(jiralink, reporter, assignee, reviewer, summary, description, storypoints)" /> <br/><br/>
</div>
<p>Output Message : {{msg}}</p>
<p>StatusCode: {{statusval}}</p>
<p>Status: {{statustext}}</p>
<p>Response Headers: {{headers}}</p>
</div>
</body>
</html>
models.py
from django.db import models
class Story(models.Model):
jiraLink = models.TextField(max_length=200)
reporter = models.TextField(max_length=50)
assignee = models.TextField(max_length=50)
Reviewer = models.TextField(max_length=50)
summary = models.TextField(max_length=50)
description = models.TextField(max_length=300, blank=True)
storyPoints = models.IntegerField(null=True, blank=True)
我无法弄清楚问题出在哪里。任何帮助都可以申请。