我有一个基于类的视图,它继承了Django Rest Framework的APIView,但当我从django视图做出反应请求时,react会引发405 Method Not Allowed。
Django View
class TakeSectionView(APIView):
def post(self, request):
return JsonResponse({'data': "hello world"})
使用Axios反应API
function fetchExamQuestion(classId, sectionId) {
const csrftoken = getCookie('csrftoken')
const url = `http://localhost:8100/exams/section/data/`;
return axios({
method: 'post',
url: url,
data: {
section_id: sectionId,
class_id: classId
},
headers: {"X-CSRFToken": csrftoken}
}).then(result => result.data);
}
以下是启动请求的ReactJS代码:
反应代码
function* fetchQuestion({ sessionId }) {
try {
const classId = yield select(getCurrentClassId);
const result = yield Api.fetchExamQuestion(classId, sessionId);
if (result.successful) {
yield put({ type: EXAM.FETCH_QUESTION_SUCCESSFUL, practiceDetail: result.data });
}
} catch (error) {
yield put({ type: EXAM.FETCH_QUESTION_FAILURE, error });
}
}
可能有帮助的另一个细节。看起来它只允许GET请求。我怎么能改变它?