2011-06-22 55 views
0

我正在使用django,并试图通过POST检索由jQuery发送的列表。POST后处理响应对象

侦错码:

$.ajax({ 
    method: 'POST', 
    url:'/coupons/sideContentCoupons/', 
    success: function(data){ 
     // access response and retrieve data. 
    } 
}); 

的处理程序:

def sideContentCoupons(request): 
    response = HttpResponse("", None, 200, "") 
    response['field'] = "data" 
    return response 

这是访问 '田' 一个基本的尝试,但我不能让 “数据”。

如何访问该字段?

回答

2

你的,你想要做什么的描述是有点迷茫,但如果我理解正确的话,它是从Django的通过Ajax发送到jQuery和访问数据在那里,你'正在问。

如果是这样,您应该考虑将数据作为JSON发送。

def handler(request): 
    return HttpResponse(simplejson.dumps({'field': 'data'})) 


$.getJSON('/coupons/sideContentCoupons/', 
    function(data) { 
     alert(data['field']); 
    }); 
0

您无法获取数据,因为您没有发送任何数据。

试试这个:

$.ajax({ 
    type: 'POST', 
    data: //this can either be a query string eg. foo=bar&bar=foo or an object 
      // {foo:'bar', bar:'foo'} 
    url:'/coupons/sideContentCoupons/', 
    success: function(data){ 
     // access response and retrieve data. 
    } 
}); 
+0

我不需要任何数据发送到处理程序,我只需要处理,它在响应 –

+0

方法=后返回数据?????它的类型不是方法 –

+0

大声笑2.5年后... type = post也不起作用。 – locrizak