2017-01-04 87 views
0

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'

我不明白爲什麼會這樣?有什麼想法嗎?

更新:

更改服務器此,現在看到NoneTypeprint(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

+0

很抱歉,但我真的有在同一呼叫嘗試很難相信這個......你可以打印'型(數據)',然後再打印按鍵。也許在兩者之間發生了變化... –

+0

您確定在錯誤發生時您正在處理POST請求嗎?你的路由是GET,POST或者OPTIONS,但是可能只有POST會包含json。 – snakecharmerb

+0

嗯,你是對的讓 - 弗朗索瓦......事情確實發生了變化。更新的問題。 –

回答

1

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

一種OPTIONS請求將不包括JSON。

編輯您的代碼只檢查JSON,如果方法是POST

例如:

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

你能提出一個關於如何做的建議嗎? –

+0

啊,好吧,我從方法中拿出了「選項」,它按預期工作。 –

+0

你能編輯這個答案,並從方法中刪除「選項」,所以它是準確的? –