我可以通过以下方式进行 GET 请求:
axios.get('/by/f')
.then(function (data) {
this.setState({list: data.data});
}.bind(this))
.catch(function (error) {
console.log(error);
});
但是,当我尝试 PUT 请求来更新我的 django 数据库时,出现 403 错误:
axios({
method: 'put',
url: '/by/f',
data: {
item: item,
frequency: frequency
}
}).then(function (response) {
console.log(response);
});
我的观点:
class FrequencyList(APIView):
def get(self, request, format=None):
frequency = Frequency.objects.all()
serializer = FrequencySerializer(frequency, many=True)
return Response(serializer.data)
def put(self, request, pk, format=None):
frequency = self.get_object(pk)
serializer = FrequencySerializer(frequency, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
我的网址模式:
urlpatterns = [
path('f', views.FrequencyList.as_view()),
]
我的标题:
headers: {
'Access-Control-Allow-Origin': '*'
},