2010-05-27 37 views
2

我和解析HTTP响应一个问题..我尝试把一些价值客户jquery; Django的;解析的HttpResponse

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

在解析时:

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

什么是从HttpResponse对象中提取值的正确方法

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

+0

你想使用JSON? – Adam 2010-05-27 20:13:05

回答

2

如果你做你的HttpResponse JSON:

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

,那么你可以接受它作为JSON

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

如果您尝试使用JSON,你可以做这样的事情:

的Django

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

并将其作为

jQuery的

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

的getJSON是jQuery的简写形式的函数到$就功能:

$.ajax({ 
    url: url, 
    dataType: 'json', 
    data: data, 
    success: callback 
}); 
+0

非常感谢!你保存我的日子 – Philip007 2013-05-28 12:31:46