This has been plaguing me for quite a while now. I've set up a basic set of apis in Django and am building an angularJS based front end. However for some weird reason the following code results in a CORS error.
var req = {
method: 'POST',
url: url,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
withCredentials: true,
param: data,
data: data
}
return $http(req).then(
function(response){
callbackSuccess(response);
},
function(response){
callbackError(response);
}
);
I found out two things that when I do a post an OPTIONS request is made and secondly none of what I post is even posted. I try to out put the request.POST contents and its empty.
Internal Server Error: /api/users/auth/
Traceback (most recent call last):
File "/home/ali/eb-virt/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/ali/eb-virt/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ali/eb-virt/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/home/ali/eb-virt/local/lib/python2.7/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/ali/Projects/api.project.local/project/api/views.py", line 50, in loginUser
emailAddress = request.POST["emailAddress"]
File "/home/ali/eb-virt/local/lib/python2.7/site-packages/django/utils/datastructures.py", line 85, in __getitem__
raise MultiValueDictKeyError(repr(key))
MultiValueDictKeyError: "'emailAddress'"
[21/Nov/2016 07:56:59] "OPTIONS /api/users/auth/ HTTP/1.1" 500 81083
And this is my server code:
def loginUser(request):
emailAddress = ''
password = ''
emailAddress = request.POST["emailAddress"]
password = request.POST["password"]
data = auth.login(emailAddress, password)
return data
I've installed the django-cors-headers and followed all the instructions to the core but its still not working...