Javascript 单击继续获取请求
该脚本在我的本地主机(本地计算机)上运行良好,但当我在上传到远程服务器后尝试它时,它会在单击按钮时无限重复请求 以下是相同的代码:Javascript 单击继续获取请求,javascript,jquery,ajax,onclick,Javascript,Jquery,Ajax,Onclick,该脚本在我的本地主机(本地计算机)上运行良好,但当我在上传到远程服务器后尝试它时,它会在单击按钮时无限重复请求 以下是相同的代码: <script src="//code.jquery.com/jquery-1.12.0.min.js"></script> <script type="text/javascript"> jQuery(function($){ $('.product-btn').click(function(){
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script type="text/javascript">
jQuery(function($){
$('.product-btn').click(function(){
if($(this).attr('data-action')=="add"){
$.ajax({url: "/product/add/cart/" + $(this).data("id") + "/from_listing/1"});
$(this).attr('data-action','remove');
$(this).children('span').html('Remove from cart');
}else{
$.ajax({url: "/product/delete/id/" + $(this).data("id") + "/"});
$(this).attr('data-action','add');
$(this).children('span').html('Add to cart');
}
});
});
</script>
<button class="product-btn btn btn-sm btn-inverse btn-embossed" data-action="add" data-id="16545"><span>Add to cart</span></button>
jQuery(函数($){
$('.product btn')。单击(函数(){
if($(this.attr('data-action')==“add”){
$.ajax({url://product/add/cart/“+$(this).data(“id”)+”/from_listing/1“});
$(this.attr('data-action','remove');
$(this.children('span').html('Remove from cart');
}否则{
$.ajax({url://product/delete/id/“+$(this.data(“id”)+“/”});
$(this.attr('data-action','add');
$(this.children('span').html('addtocart');
}
});
});
添加到购物车
我在提供的代码中没有看到任何会导致无限循环的内容。也许可以尝试在ajax帖子中添加一些console.log
消息和一个成功与失败处理程序,看看您是否可以捕获任何可以创建JSFIDLE或plnkr来演示的内容?success
在js
的另一部分中对$.ajax()
的回调是否未包含在问题中?@zgood@guest271314是否可能与prototype.js发生冲突?我得到uncaughttypeerror:无法在我的控制台中读取未定义的prototype.js:115的属性“insert”。如何避免冲突,因为我需要jQuery和prototype。@KevinS