I'm not sure why I get this error. I have favorite app, and ajax must be falling apart. because this error occurs when I click a button that should work but not working right now. my guess is I;m missing some csrf or cookie or wrong jquery version.... I'm not sure. This is my code
and in html file
<div class="actions">{% if user.is_authenticated %}{% fav_item category user %}{% endif %}</div>
</div>
I'll post views.py as well
def ajax_login_required(view_func):
def wrap(request, *args, **kwargs):
if request.user.is_authenticated():
return view_func(request, *args, **kwargs)
json = simplejson.dumps({'not_authenticated': True})
return HttpResponse(json, content_type='application/json', status=401)
wrap.__doc__ = view_func.__doc__
wrap.__dict__ = view_func.__dict__
return wrap
@ajax_login_required
def ajax_fav(request, ctype_id, obj_id):
"""
"""
ctype = get_object_or_404(ContentType, pk=ctype_id)
item = ctype.get_object_for_this_type(pk=obj_id)
if Favorite.objects.filter(user=request.user, content_type=ctype, object_id=obj_id):
fav = Favorite.objects.get(user=request.user, content_type=ctype, object_id=obj_id)
fav.delete()
count = Favorite.objects.favorites_for_object(item).count()
data_dict = {'id': 0, 'message': fav_settings.FAV_ADD, 'counter': build_message(count), }
else:
fav = Favorite.objects.create_favorite(item, request.user)
count = Favorite.objects.favorites_for_object(item).count()
data_dict = {'id': fav.id, 'message': fav_settings.FAV_REMOVE, 'counter': build_message(count), }
return HttpResponse(simplejson.dumps(data_dict), content_type='application/javascript')
Edit: /In Console I get js error Uncaught SyntaxError: Unexpected identifier
$(function(){
$('a.favIt').on('click', function(){
var itemId = $(this).attr('id').split("_")[1];
$.ajax({
type: "POST",
url: $(this).attr("href"),
data: {csrfmiddlewaretoken: '{{ csrf_token }}'}
dataType: "json",
timeout: 2000,
cache: false,
beforeSend: function(XMLHttpRequest) {
//$("#loader").fadeIn();
},
error: function(data, XMLHttpRequest, textStatus, errorThrown){
$(this).html("Error connecting to the server.");
},
complete: function(XMLHttpRequest, textStatus) {
//$("#loader").fadeOut();
},
success: function(data, textStatus, XMLHttpRequest){
$('#FavIt_'+itemId).html(data.message);
$('#FavCounter_'+itemId).html(data.counter);
}
});
return false;
});
});
it says its occuring from line 8 which is dataType: "json", I posted views.py