Reputation: 3548
The AJAX call looks like this:
json_dictionary = {url : url, profile_dict: dictionary, final_dict : final_dictionary}
$.ajax({
url: "https://localhost:8090/add_profile_data",
type: "POST",
data: JSON.stringify(json_dictionary),
contentType:'application/json',
success:function() {
console.log('added profile data')
}
})
The client side looks like this:
@app.route('/add_profile_data', methods=['POST', 'GET', 'OPTIONS'])
def add():
data = request.json
print(type(data))
That works, and the result from print is <type 'dict'>
Also, when I print the data
object, everything is there.
However, when I try:
@app.route('/add_profile_data', methods=['POST', 'GET', 'OPTIONS'])
def add():
data = request.json
print(data.keys())
I get an error: AttributeError: 'NoneType' object has no attribute 'keys'
I don't understand why this is happening? Any thoughts?
Update:
Changed the server to this, and now seeing NoneType
for print(type(data))
Server:
@app.route('/add_profile_data', methods=['POST', 'GET', 'OPTIONS'])
def add():
data = request.json
print(type(data))
print(data.keys())
Response:
<type 'NoneType'>
127.0.0.1 - - [04/Jan/2017 09:13:47] "OPTIONS /add_profile_data HTTP/1.1" 500 -
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/flask/app.py", line 2000, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Library/Python/2.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/Library/Python/2.7/site-packages/flask/app.py", line 1567, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Python/2.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/Library/Python/2.7/site-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/morganallen/Dropbox/Coding_Projects/Prediction_Server/prediction_v2.py", line 172, in add
print(data.keys())
AttributeError: 'NoneType' object has no attribute 'keys'
Any reason why I am seeing NoneType
?
Upvotes: 0
Views: 248
Reputation: 55933
127.0.0.1 - - [04/Jan/2017 09:13:47] "OPTIONS /add_profile_data HTTP/1.1" 500
An OPTIONS
request will not include JSON.
Edit your code to only check the JSON if the method is POST
.
For example:
@app.route('/add_profile_data', methods=['POST', 'GET'])
def add():
if request.method == 'POST':
data = request.json
print(type(data))
print(data.keys())
Upvotes: 1