django ajax返回,使用Django从AJAX调用返回字典

我正在进行AJAX调用,并希望它返回对象字典。无论我如何尝试返回数据,它都会引发异常。据我所知,这只能是因为响应数据不是JSON格式。使用Django从AJAX调用返回字典

任何帮助将非常感激

def populateSources(request):

if request.is_ajax():

try:

org = Organization.objects.get(pk=int(request.GET.get(org_id)))

std_source_columns = StandardizedSourceColumn.objects.all()

std_sources = StandardizedSource.objects.all()

# Standardized Tables API Client

std_tables_api_client = standardizedtablescli.ApiClient()

std_tables_api_client.host = os.environ.get(STANDARDIZED_ENDPOINT)

std_tables_api = standardizedtablescli.StandardizedtablesApi(std_tables_api_client)

org_std_sources = std_tables_api.get_standardized_tables_by_id(org.id)

ready_tables = std_tables_api.get_ready_raw_tables(org.id)

ready_table_mapping = dict()

ready_table_names = []

for table in ready_tables:

ready_table_names.append(table)

for key, value in org_std_sources.iteritems():

curr_source = StandardizedSource.objects.filter(name=key)

if len(value[standard_mappings]) == 0:

if key in ready_table_names:

ready_table_mapping[curr_source] = False

else:

ready_table_mapping[curr_source] = True

json_response = {}

json_response[result] = ready_table_mapping

return HttpResponse(

json.dumps(json_response),

content_type="application/json"

)

except:

return HttpResponse(

json.dumps("error"),

content_type="application/json"

)

+0

什么是例外? –

+4

摆脱try..except块。你会知道错误是什么。另外,它是一个bd编程实践,可以在代码为 –

我正在进行AJAX调用,并希望它返回对象字典。无论我如何尝试返回数据,它都会引发异常。据我所知,这只能是因为响应数据不是JSON格式。使用Django从AJAX调用返回字典 任何帮助将非常感激 def populateSources(request): if request.is_ajax(): try: org = Organization.objects.get(pk=int(request.GET.get(org_id))) std_source_columns = StandardizedSourceColumn.objects.all() std_sources = StandardizedSource.objects.all() # Standardized Tables API Client std_tables_api_client = standardizedtablescli.ApiClient() std_tables_api_client.host = os.environ.get(STANDARDIZED_ENDPOINT) std_tables_api = standardizedtablescli.StandardizedtablesApi(std_tables_api_client) org_std_sources = std_tables_api.get_standardized_tables_by_id(org.id) ready_tables = std_tables_api.get_ready_raw_tables(org.id) ready_table_mapping = dict() ready_table_names = [] for table in ready_tables: ready_table_names.append(table) for key, value in org_std_sources.iteritems(): curr_source = StandardizedSource.objects.filter(name=key) if len(value[standard_mappings]) == 0: if key in ready_table_names: ready_table_mapping[curr_source] = False else: ready_table_mapping[curr_source] = True json_response = {} json_response[result] = ready_table_mapping return HttpResponse( json.dumps(json_response), content_type="application/json" ) except: return HttpResponse( json.dumps("error"), content_type="application/json" ) +0 什么是例外? – +4 摆脱try..except块。你会知道错误是什么。另外,它是一个bd编程实践,可以在代码为 –
经验分享 程序员 微信小程序 职场和发展