0

here is my post method code in react

const URL = `http://localhost:8000`
export default URL;

export function addRecruiterRegister(values,cb){
    const request=fetch(`${URL}/recruiterRegister`,{
        method:'POST',
        mode: "cors",
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(values)
        }).then(
          //  () => cb()
        )

        return {
            type:'ADD_RECRUITER',
            payload:'everything went fine'
    }
}

here is the code in django-python

class RecruiterRegisterList(generics.ListAPIView):

    queryset=RecruiterRegister.objects.all()
    serializer_class = RecruiterRegisterSerializer
    filter_backends = (DjangoFilterBackend,)
    filter_fields=('email','password')

    def post(self, request, format=None):
        serializer = RecruiterRegisterSerializer(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)

    def delete(self, request, pk, format=None):
        recruiterRegisters = self.get_object()
        recruiterRegisters.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

error in the browser console:

Access to fetch at 'http://localhost:8000/recruiterRegister' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request. :3000/#/forms/recruiter-register-form:1 Uncaught (in promise) TypeError: Failed to fetch

and in python console i am getting this when i click submit:

"OPTIONS /recruiterRegister HTTP/1.1" 301 0

very new to django-restframework and reactjs, please help!!

1
  • Although get method is working Commented Jan 26, 2019 at 13:46

1 Answer 1

1

I guess the problem caused by generics.ListAPIView. This is only have get method. If you want to make post request also, you should use generics.CreateAPIView.

class RecruiterRegisterList(generics.CreateAPIView,
                            generics.ListAPIView):

    queryset=RecruiterRegister.objects.all()
    serializer_class = RecruiterRegisterSerializer
    filter_backends = (DjangoFilterBackend,)
    filter_fields=('email','password')

    ...
Sign up to request clarification or add additional context in comments.

3 Comments

no it is not working.Although i could post through postman before also.
Can you change your url ${URL}/recruiterRegister to ${URL}/recruiterRegister/?
lastly, have you ever check this and try to do post request with postman?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.