我试图在 django 网站中使用 ajax 来实现简单的类似部分。单击“赞”按钮会喜欢该帖子,但 html 不会更改。如果我刷新页面,html 就会发生变化。
控制台显示以下错误
Uncaught ReferenceError: response is not defined at Object.success ((index):271) at u (jquery.min.js:2) at Object.fireWith [as resolveWith] (jquery.min.js:2) at k (jquery.min.js:2) at XMLHttpRequest. (jquery.min.js:2)
下面给出了 JavaScript
<script type="text/javascript">
$(document).ready(function(event){
$(document).on('click','#like', function(event){
event.preventDefault();
var pk= $(this).attr('value');
$.ajax({
type:'POST',
url:'{% url "like_post" post.id %}',
data:{'blog_id':pk,'csrfmiddlewaretoken':'{{ csrf_token}}'},
dataType:'json',
success: function(event){
$('#like-section').html(response['form'])
},
fail:function(rs, e){
console.log(rs, responseText);
},
});
});
});
</script>
该部分的 html
<div>
<form action="{% url 'like_post' post.id %}">
{% csrf_token %}
{% if is_liked %}
<button id="like" type='submit' name='blog_id' value="{{ post.id }}" class="btn ">unlike</button>
{% else %}
<button id="like" type='submit' name='blog_id' value="{{ post.id }}" class="btn ">like</button>
{% endif %}
</form>
</div>
以下是html页面的代码
<div id="like-section">
{% include 'blog/like_section.html' %}
</div>
最佳答案
您的变量“响应”未定义。您的 ajax 成功处理程序有“事件”,将“事件”更改为“响应”,它应该可以工作。
success: function(response){
$('#like-section').html(response['form'])
},
关于javascript - Uncaught ReferenceError : response is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55268839/