如何在特定 div 上显示 ajax 响应?此语句有效alert(data[i]['title']);
,但我想在 div 上显示。谢谢
<td><a onclick="ajax_request('{% url recommendation id=track.id %}');" href="#"> Click Here</a></td>
阿贾克斯函数
function ajax_request(url) {
$.ajax({
url: url,
type: 'POST',
data: {csrfmiddlewaretoken: '{{ csrf_token }}'},
success: function(data) {
for(i=0;i<=data.length;i++){
alert(data[i]['title']);
}
}
})
}
看法
@login_required
def recommendation(request, id):
if request.is_ajax() and request.method == 'POST':
track = Track.objects.get(pk=id)
recommend = Song.objects.filter(title__icontains=track.title)[:5]
results = []
for data in recommend:
results.append({'title': data.title})
json = simplejson.dumps(results)
return HttpResponse(json, mimetype="application/json")