jQuery str.split不起作用

时间:2013-10-28 17:32:16

标签: jquery python ajax

我在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)

1 个答案:

答案 0 :(得分:1)

使用json library

返回JSON响应
import json

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

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

$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});