-1

我在 AJAX 调用中通过服务器发送一个列表(在 python 中)作为响应。

回应是[u"<select id = '1_level1'><option>Select Region </option><option>Asia / Pacific</option><option>North America</option></select>", u"<select id = '2_level1'><option>Select Region </option><option>Asia / Pacific</option><option>North America</option></select>"]

当我使用

success: function(result){

          $("#compare1").html(result.split(",")[0]);

它打印两个选择选项。如何拆分列表?

已编辑

elif request.POST.has_key('compare_options'):

            #user_organization_id = 
            compare_id = request.POST.getlist('id[]')
            level1_options = Level1Detail.objects.filter(organization_id = 1) # change it with user_organization_id

            option = []

            for ids in compare_id:
                option_str = ""
                level1_options_str = "<option>Select Region </option>"
                for options in level1_options:
                    level1_options_str += "<option>"+options.level1_name+"</option>"
                option_str = "<select id = '"+ids+"level1'>" + level1_options_str + "</select>"
                option.append(option_str)

            print option


            return HttpResponse(option)
4

1 回答 1

1

json而是使用库返回 JSON 响应:

import json

return HttpResponse(json.dumps(option), content_type="application/json")

在 jQuery 中,确保告诉它接收 JSON:

$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});
于 2013-10-28T17:55:57.530 回答