AJAX调用看起来是这样的:与AJAX POST获取JSON数据问题
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')
}
})
客户端看起来是这样的:
@app.route('/add_profile_data', methods=['POST', 'GET', 'OPTIONS'])
def add():
data = request.json
print(type(data))
这样的作品,并从打印结果是<type 'dict'>
此外,当我打印data
对象时,一切都在那里。
然而,当我尝试:
@app.route('/add_profile_data', methods=['POST', 'GET', 'OPTIONS'])
def add():
data = request.json
print(data.keys())
我得到一个错误:AttributeError: 'NoneType' object has no attribute 'keys'
我不明白为什么会这样?有什么想法吗?
更新:
更改服务器此,现在看到NoneType
为print(type(data))
服务器:
@app.route('/add_profile_data', methods=['POST', 'GET', 'OPTIONS'])
def add():
data = request.json
print(type(data))
print(data.keys())
响应:
<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'
任何原因,我看到NoneType
?
很抱歉,但我真的有在同一呼叫尝试很难相信这个......你可以打印'型(数据)',然后再打印按键。也许在两者之间发生了变化... –
您确定在错误发生时您正在处理POST请求吗?你的路由是GET,POST或者OPTIONS,但是可能只有POST会包含json。 – snakecharmerb
嗯,你是对的让 - 弗朗索瓦......事情确实发生了变化。更新的问题。 –