2014-10-09 56 views
0

我有jQuery的解析錯誤200的Json AJAX webapp2的

function Admin_Ajax_pop_rows(){ 
$(document).ready(function(){ 
    variable1= 'none'; 
     $.ajax({ 
      type: "POST", 
      url: "/someurl", 
      dataType: "json", 
      data: JSON.stringify({"variable1": variable1}) 
     }) 
     .success(function(data){ 
      alert('success response: ' + data + ' number of rows : '); 
     }) 
     .done(function(data){ 
      alert ('rows : ' + data.return_rows); 
      MakeTablejQuery(data); 
     }) 
     .fail(function(error){ 
      alert('error status is : ' + error.status + ' text: ' + error.statusText + ' response  text : ' + error.responseText); 
     }); 
    }); 
} 

而且在我的Python服務器代碼我有

def post(self): 
    user_key = ndb.Key(self.user_model,'value') 
    user_key_parent = user_key.parent() 
    user_query = self.user_model.query(ancestor = user_key_parent).order(ndb.GenericProperty(sort_field)) 
    query_data = user_query.fetch(i, projection=[ndb.GenericProperty('name'),ndb.GenericProperty('last_name'),ndb.GenericProperty('email_address')]) 
    table_prop_data = { 'return_rows': 9 , 'return_cols' : 8} 
    return_table_prop_data = [] 
    return_table_prop_data = json.dumps(table_prop_data) 
    return_data = [] 
    return_data = json.dumps([dict(p.to_dict(), **dict(id=p.key.id())) for p in query_data],default = date_handler) 
    self.response.headers['content-type']=("application/json;charset=UTF-8") 
    self.response.out.write(return_data) 
    self.response.headers['content-type']=("application/json;charset=UTF-8") 
    self.response.out.write(return_table_prop_data) 

我得到的 「200」 錯誤與「狀態的AJAX調用解析錯誤」

JSONLint示出了JSON錯誤

Parse error on line 74: 
    ...662981951488 }]{ "return_cols": 
    ---------------------^ 
    Expecting 'EOF', '}', ',', ']' 

我使用webapp2的GAE上

每Felix的建議,我試圖創建一個字典使用下列 -

return_data = json.dumps({'table_props': dict(table_prop_data), 'query_data' : [dict(p.to_dict(), **dict(id=p.key.id())) for p in query_data],default = date_handler}) 

我得到一個語法錯誤。請幫我解決這個問題。這是我的date_handler函數。我需要這個照顧我查詢中的日期時間字段。

def date_handler(obj): 
    return obj.isoformat() if hasattr(obj, 'isoformat') else obj 
+0

順便說一下,200不是錯誤代碼。這是HTTP OK代碼。從AJAX調用者的角度來看,HTTP調用成功了,只是JSON格式不正確,正如下面的@FelixKling回答中所述。 – 2014-10-09 13:58:06

+0

Mike - 同意。 JSON達到了我的javascript,並且在我的Chrome開發人員工具中,我能夠訪問對象列表(query_data)和變量列表(table_prop_data)。但AJAX返回一個.fail,我無法執行.success下的功能。你是正確的,因爲json格式不正確,所以ajax調用失敗。 – 2014-10-09 15:04:57

+0

關於你的語法錯誤,似乎你沒有正確關閉傳遞給'json.dumps'的字典。 '''','default'=''''''''''後面缺少'}'。錯誤信息應該已經引導您朝着正確的方向發展。 – 2014-10-09 15:06:53

回答

1

您似乎試圖在單個響應中返回兩個單獨的JSON blob。這是行不通的,正如你可以通過jsonlint錯誤看到的那樣。整個響應必須是單個JSON blob。

+0

你可以請幫助如何創建這兩個單一的JSON blob?我必須合併/附加對象列表(query_data)和鍵值(table_prop_data)。我在這裏掙扎。任何幫助是極大的讚賞。我需要將這些數據集(query_Data和table_prop_data)都放到我的javascript中,並在成功的AJAX調用之後訪問它們。 – 2014-10-09 13:44:14

+0

創建一個字典或一個列表並對其進行字符串化:'json.dumps({'table_props':table_prop_data,'query':[...]})'。您可以以任何對您有意義的方式組合這兩個值。 – 2014-10-09 13:52:07

+0

謝謝菲利克斯。讓我試試這個,回到你身邊。 – 2014-10-09 14:10:49