2

我在解析 http 响应时遇到问题。我尝试向客户端发送一些值

>>>>return HttpResponse(first=True,second=True)

解析时:

$.post('get_values',"",function(data){
                alert(data['first']); //The alert isn't shown!!!
            });

从httpresponse中提取值的正确方法是什么

也许我在创建回复时犯了一个错误..

4

2 回答 2

8

如果您尝试使用 json,则可以执行以下操作:

姜戈

data = json.dumps({"FIRST":True, "SECOND":False})
    return HttpResponse(data, mimetype="application/json")

并得到它:

jQuery

$.getJSON(url, [data], function(data){
                alert(data['first']);
            });

getJSON 是相当于 $.ajax 函数的 jquery 速记函数:

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
});
于 2010-05-27T20:30:08.827 回答
2

如果您制作了 HttpResponse json:

return HttpResponse("{\"first\":true,
\"second\":false}")

那么您可以将其作为 json 接收

$.post('get_values',"",function(data){
                alert(data['first']); //The alert isn't shown!!!
            },"json");
于 2010-05-27T20:20:18.827 回答