Reputation: 99
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!!
Upvotes: 0
Views: 534
Reputation: 1714
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')
...
Upvotes: 1