javascript - 如何将ajax代码转换为django中的vue.js axios方法
问题描述
如果用户点击喜欢按钮网页不刷新并且喜欢的计数也更新而不刷新所以,首先我用ajax编写然后我想在转换为vue.js后将其转换为vue.js它不起作用。任何人都可以帮助我实现这一目标。
这里我写了 vue.js 的 axios 方法。我在 html 模板中访问过的 html 模板
<div id = "deals-vikreya">
<a class="likebutton" style="
{% if request.session.email %}pointer-events: auto;
{% else %}pointer-events: none;{% endif %}" id="like-{{i.id}}" href="#"
data-catid="{{ i.id }}">
<i class="fa fa-thumbs-o-up" id="like-icon-{{i.id}}" aria-hidden="true"
style="color: gray"></i>
</a>
<span id="cm_count{{i.id}}">
({{ c_count }})
</span>
</div>
vue.js axios
<script>
new Vue({
el: '#deals-vikreya',
data() {
return {};
methods: {
$('.likebutton').click(function(){
var catid;
catid = $(this).attr("data-catid");
console.log(catid);
axios(
{
method : "POST",
url: "{% url 'post_comment_like' + catid %}", //django path name
data:{
post_id: catid
},
success: function( data )
{
console.log("like saved");
console.log(data.comment_count);
document.getElementById("cm_count"+catid).innerHTML ="(" + data.comment_count + ")";
$( '#like-'+ catid ).attr("href", "/post_comment_like_remove/" + catid);
$( '#like-icon-'+ catid ).css("color", "#DB0038");
}
})
});
},
})
</script>
解决方案
推荐阅读
- r - r expss 重新编码因子
- reactjs - 'rotate' 没有定义 React.JS
- javascript - 根据 Google 表单字段的特定结果自定义 Discord 上的嵌入式消息颜色输出
- python - 拆分文本,但使用正则表达式将分隔符保留在句末
- python - 如何使用 Python 运行大型 SQL 查询并导出为 CSV?
- php - 如何在 PHP 中切断循环
- apache2 - 刷新apache2中的子目录时出错
- javascript - 关于如何删除 HTML 下拉项元素的困惑
- parsing - 添加继承属性以解决语法中的语义问题
- java - Spring-ws 不全局添加拦截器