我有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
順便說一下,200不是錯誤代碼。這是HTTP OK代碼。從AJAX調用者的角度來看,HTTP調用成功了,只是JSON格式不正確,正如下面的@FelixKling回答中所述。 – 2014-10-09 13:58:06
Mike - 同意。 JSON達到了我的javascript,並且在我的Chrome開發人員工具中,我能夠訪問對象列表(query_data)和變量列表(table_prop_data)。但AJAX返回一個.fail,我無法執行.success下的功能。你是正確的,因爲json格式不正確,所以ajax調用失敗。 – 2014-10-09 15:04:57
關於你的語法錯誤,似乎你沒有正確關閉傳遞給'json.dumps'的字典。 '''','default'=''''''''''後面缺少'}'。錯誤信息應該已經引導您朝着正確的方向發展。 – 2014-10-09 15:06:53