Reputation: 7493
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...
Upvotes: 0
Views: 137
Reputation: 20369
OPTION
request is doing by your browser for a safety whether it can send POST request(in case of cross domain). It should return a 200 with some headers(will take care of cors-headers
middleware if you added it).
So In server, accept POST
request this way(assume function based views).
if request.method == 'POST':
#access POST variables here.
Upvotes: 1
Reputation: 7493
Found the issue - was a total noob mistake.
My version of Django in my virtual environment was 1.9 whereas I had generated the project from a version 1.10 so in the settings the MIDDLEWARE
option had to be named as MIDDLEWARE_CLASSES
- silly mistake had me stuck for two days - thanks everyone for all your answers :)
Upvotes: 0
Reputation: 8526
The problem is in Line :
emailAddress = request.POST["emailAddress"]
In the place of this line,use this code:
def loginUser(request):
if request.method == 'POST':
emailAddress = request.POST.get('emailAddress')
password = request.POST.get('password')
data = auth.login(emailAddress, password)
return data
request.POST["emailAddress"] will raise a KeyError exception if 'emailAddress' is not in request.POST.
While request.POST.get('emailAddress') will return None if 'emailAddress' is not in request.POST.
Upvotes: 2