从AJAX POST获取JSON数据的问题

时间:2017-01-04 14:06:49

标签: python ajax

AJAX调用如下所示:

        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的任何原因?

1 个答案:

答案 0 :(得分:1)

127.0.0.1 - - [04/Jan/2017 09:13:47] "OPTIONS /add_profile_data HTTP/1.1" 500

OPTIONS请求不包含JSON。

如果方法为POST,请编辑代码以仅检查JSON。

例如:

@app.route('/add_profile_data', methods=['POST', 'GET'])
def add():
    if request.method == 'POST':
        data = request.json
        print(type(data))
        print(data.keys())