Ajax无法激活,将我带到其他页面 [英] ajax not activating, taking me to a different page
问题描述
代码按我的意愿工作,但它带我到另一个页面,并且有类似{"success":1,"voteobj":57}的内容.我被告知ajax就是为此而设计的,但我并不擅长.这就是我所拥有的.有人可以检查我在哪里出错了,以及应该怎么做才能解决它?
the code works as I want but it takes me to a different page and have something like {"success": 1, "voteobj": 57}. I was informed ajax is the one for this but I'm not good at it. here's what I have. can someone check where I made an error and what I should do to fix it/?
<script type="text/javascript">
jQuery(document).ready(function($)
{
$(".vote_form").submit(function(e)
{
e.preventDefault();
var btn = $("button", this);
var l_id = $(".hidden_id", this).val();
btn.attr('disabled', true);
$.post("vote/", $(this).serializeArray(),
function(data) {
if(data["voteobj"]) {
btn.text("-");
}
else {
btn.text("+");
}
});
btn.attr('disabled', false);
});
});
</script>
<script
src="https://code.jquery.com/jquery-3.4.1.js"
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous"></script>
<form method="post" action="{% url 'vote' %}" class="vote_form">
<li> [{{ post.votes }}]
{{post}}
{% csrf_token %}
<input type="hidden" id="id_post" name="post" class="hidden_id" value="{{ post.pk }}" />
<input type="hidden" id="id_voter" name="voter" class="hidden_id" value="{{ user.pk }}" />
{% if not user.is_authenticated %}
<p>login to vote</p>
{% elif post.pk not in voted %}
<button class="btn btn-primary">like</button>
{% else %}
<button class="btn btn-primary">dislike</button>
{% endif %}
</form>
以下是我的python代码
And following is my python code
class JSONFormMixin(object):
def create_response(self, vdict=dict(), valid_form=True):
response = HttpResponse(json.dumps(vdict), content_type='application/json')
response.status = 200 if valid_form else 500
return response
class VoteFormBaseView(FormView):
form_class = VoteForm
def create_response(self, vdict=dict(), valid_form=True):
response = HttpResponse(json.dumps(vdict))
response.status = 200 if valid_form else 500
return response
def form_valid(self, form):
post = get_object_or_404(Post, pk=form.data["post"])
user = self.request.user
prev_votes = Vote.objects.filter(voter=user, post=post)
has_voted = (len(prev_votes) > 0)
ret = {"success": 1}
if not has_voted:
# add vote
v = Vote.objects.create(voter=user, post=post)
ret["voteobj"] = v.id
else:
# delete vote
prev_votes[0].delete()
ret["unvoted"] = 1
return self.create_response(ret, True)
def form_invalid(self, form):
ret = {"success": 0, "form_errors": form.errors }
return self.create_response(ret, False)
urls.py
urlpatterns = [
path('vote/', auth(VoteFormView.as_view()), name='vote'),
I followed the instruction on the following web: https://arunrocks.com/building-a-hacker-news-clone-in-django-part-4/
一切都很好,但ajax在我的身边
Everything works good but ajax on my side
因此,没有ajax的网址是 http://127.0.0.1:8000/community/投票/使用ajax时,URL为/community/post/3/vote/但它显示未找到:在终端
So without ajax the url is http://127.0.0.1:8000/community/vote/ With ajax the url is /community/post/3/vote/ but it says Not Found: on terminal
推荐答案
return self.create_response(ret, True)
应该是
return JsonResponse(safe=False, ret)
对不起,我没花时间懒洋洋地回答.不要更改响应.保持它不变,然后执行我上面刚刚做的事情.
Sorry, I didn't take my time and answered lazily. Don't change response. Keep that as you have it and do what I just did above.
应该工作.
这篇关于Ajax无法激活,将我带到其他页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!