gpt4 book ai didi

jquery - $.ajax({}) 不是 django 中的函数

转载 作者:太空宇宙 更新时间:2023-11-03 14:44:32 27 4
gpt4 key购买 nike

这样的相关问题我见过很多,但没有一个能解决我的问题。还有其他原因导致这不起作用,但我觉得很难找出问题所在。这是代码

base.html

<div class="sections-wrapper">
{% block pagecontent %}
{% endblock pagecontent %}
</div>
{% block js %}
{% include 'includes/js.html' %}
<script>
$(document).ready(function(){
function updateText(btn, newCount, verb){
btn.text(newCount + " " + verb)
}
$(".like-company").click(function(e){
e.preventDefault()
var this_ = $(this)
var likeUrl = this_.attr("data-href")
var likeCount = parseInt(this_.attr("data-likes")) | 0
var addLike = likeCount + 1
var removeLike = likeCount - 1
if (likeUrl){
$.ajax({
url: likeUrl,
method: "GET",
data: {},
success: function(data){
console.log(data)
var newLikes;
if (data.liked){
updateText(this_, addLike, "Unlike")
} else {
updateText(this_, removeLike, "Like")
}
}, error: function(error){
console.log(error)
console.log("error")
}
})
}
})
})
</script>
{% endblock js %}

包含/js.html

{% load static %}
<script src="{% static 'js/jquery.slim.min.js'%}"></script>
<script src="{% static 'js/tether.min.js' %}"></script>
<script src="{% static 'js/bootstrap4.min.js'%}"></script>

home.html

{% extends 'base.html' %}
{% block pagecontent %}
<div class="container">
{% if companies %}
<h3 class="text-center bold categories">COMPANIES</h3>
{% for company in companies %}
{% if forloop.first %}<div class="row">{% endif %}
<a href="{{ company.get_like_url }}" class="btn btn-link like-company" data-href="{{ company.get_like_url }}" data-likes="{{ company.likes.count }}"><i class="fa fa-thumbs-o-up"></i>{{ company.likes.count }} like</a>
</div>
{% endfor %}
{% endif %}
</div>
{% endblock pagecontent %}

我已经检查了 jquery 是否已加载,并且代码正在运行到 if (likeUrl)

最佳答案

您在可以阅读的说明中使用了jquery slim

/*! jQuery v3.2.1 -ajax,-ajax/jsonp,-ajax/load,-ajax/parseXML,-ajax/script,-ajax/var/location,-ajax/var/nonce,-ajax/var/rquery,-ajax/xhr,-manipulation/_evalUrl,-event/ajax,-effects,-effects/Tween,-effects/animatedSelector | (c) JS Foundation and other contributors | jquery.org/license */

slim 版本中不包含 ajax

更多release-note

关于jquery - $.ajax({}) 不是 django 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46345388/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com