我有一个与休息框架相连的角度js应用程序。我试图发布一些数据,以便对其余框架进行如下更改:
$scope.addDegree = function(degree) {
$http.post('/api/studentacademicprogram/', degree).then(function(response){
alert('post added');
}, function(response){
alert('post not added');
});
};
以下是我如何定义我从工厂获得的信息:
angular.module('jsonService', ['ngResource'])
.factory('ProgramsService',function($http) {
return {
getItems: function(callback) {
$http.get('/api/studentacademicprogram/?format=json').success(callback);
}
};
})
以下是我在其余框架中定义视图的方式:
class StudentAcademicProgramList(APIView):
def get(self, request, format=None):
student_academic_program = Student_academic_program.objects.filter(student=29722)
serialized_Student_academic_program = StudentAcademicProgramSerializer(student_academic_program, many=True)
return Response(serialized_Student_academic_program.data)
def post(self, request, format=None):
serializer = StudentAcademicProgramSerializer(data = request.DATA)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status= status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class StudentAcademicProgramDetail(APIView):
def get_objects(self, pk):
try:
return Student_academic_program.objects.get(pk=pk)
except Student_academic_program.DoesNotExist:
raise Http404
def get(self, request, pk, format=None):
student_academic_program = self.get_objects(pk)
serialized_Student_academic_program = StudentAcademicProgramSerializer(student_academic_program)
return Response(serialized_Student_academic_program.data)
但是当我尝试发布一些数据时。它在控制台中抛出一个POST错误:
POST http://127.0.0.1:8000/api/studentacademicprogram/ 400 (BAD REQUEST)
在@EliteOctagon建议添加控制台后,我在控制台中收到以下内容:
Object {data: Object, status: 400, headers: function, config: Object}
config: Object
data: Object
headers: Object
Accept: "application/json, text/plain, */*"
Content-Type: "application/json;charset=utf-8"
__proto__: Object
method: "POST"
transformRequest: Array[1]
transformResponse: Array[1]
url: "/api/studentacademicprogram/"
__proto__: Object
data: Object
academic_program: Array[1]
student: Array[1]
__proto__: Object
headers: function (name) {
status: 400
__proto__: Object
答案 0 :(得分:0)
尝试这样的事情,在使用post
时以json格式定义发送的变量$scope.addDegree = function(degree) {
var variablesToSend = {'degree': degree}
$http.post('/api/studentacademicprogram/', variablesToSend ).then(function(response){
console.log(response);
alert('post added');
}, function(response){
console.log(response);
alert('post not added');
});
};