405方法不允许ReactJS发布到Django

时间:2018-02-12 20:57:33

标签: reactjs django-rest-framework django-urls axios react-router-v4

我有一个基于类的视图,它继承了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 });
  }
}

以下是我在控制台中看到的错误: enter image description here

可能有帮助的另一个细节。看起来它只允许GET请求。我怎么能改变它?

enter image description here

0 个答案:

没有答案