EDITEDBắt dữ liệu trở lại từ django views.py và sử dụng ajax để hiển thị nó
Tôi đang cố gắng sử dụng jquery/ajax để hiển thị dữ liệu trả về từ một phương pháp django.
Tôi có nút html có tên keywordBtn. Vì vậy, khi nó được nhấn, phương thức updateKeywordSubscribed sẽ được gọi.
tuy nhiên, đối tượng của tôi không được trả lại bởi django. Có gì sai với phương pháp của tôi không?
Nếu thành công, tên phần div "update" sẽ hiển thị danh sách các từ trong danh sách json đó.
những gì tôi có trong html của tôi:
<script type="text/javascript">
$(document).ready(function() {
$("#keywordBtn").click(function(e) {
updateKeywordSubscribed(e, "#keywords");
});
});
function updateKeywordSubscribed(e, keywords) {
e.preventDefault();
var option_form = jQuery(e.target);
$.ajax({
url : option_form.attr('action'),
type : option_form.attr('method'),
data : option_form.serialize(),
dataType : 'json',
success : function(response) { alert ('sometext')})
}
</script>
những gì tôi có trong views.py tôi:
def keyword_subscribe(request):
if 'keyword_input' in request.POST:
if 'name_input' in request.POST:
xhr = request.GET.has_key('xhr')
response_dict = {}
new_keyword = request.POST['keyword_input']
username = request.POST['name_input']
response_dict.update({'keyword_input': new_keyword, 'name_input': username})
power_keyword = subscribe_keyword(username,keywords)
if power_keyword:
response_dict.update({'success':True})
else:
response_dict.update({'errors':{}})
if not username:
response_dict['errors'].update({'name_input': 'User ID is required'})
if not total and total is not False:
response_dict['errors'].update({'keyword_input': 'Keyword field is blank'})
if xhr:
return HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
return render_to_response('r2/userprofile_list.html', response_dict)
tôi chỉ cần hiểu cách jquery đọc dữ liệu đó từ views.py. Phần còn lại tôi sẽ có thể điền vào. –