$ .ajax在onclick事件中无效并绕过

时间:2022-12-05 14:13:31

My onclick event doesn't call ajax request, However function gives me both first and second alert. here is my code.

我的onclick事件不会调用ajax请求,但是函数会给我第一个和第二个警报。这是我的代码。

 function interaction()
    {
        alert('first');

        $.ajax({
                    url: "store_setting_ajax.php",
                    dataType:'json',
                    type:'POST',
                    data:{
                        interaction1:'yes'
                    },
                    success:function(data){
                        window.location.reload();

                    }
                });
       alert('second');
    }

I try e.preventDefault() but not working.

我尝试e.preventDefault()但不工作。

1 个解决方案

#1


0  

If it's a form, then put it into it's onsubmit like this: <form method='POST' action='' onsubmit='return interaction()'> And at the functions end, do a return false;

如果它是一个表单,那么把它放在提交中就像这样:

在函数结束时,返回false;

type:'POST' is kinda deprecated, use method:'POST' instead. Instead of successuse done like this:

类型:'POST'有点弃用,使用方法:'POST'代替。而不是成功使用这样做:

$.ajax({....... your stuff .......}).done(function(data){
window.location.reload(); //whats the point of using ajax here, if you do a reload then?
})

#1


0  

If it's a form, then put it into it's onsubmit like this: <form method='POST' action='' onsubmit='return interaction()'> And at the functions end, do a return false;

如果它是一个表单,那么把它放在提交中就像这样:

在函数结束时,返回false;

type:'POST' is kinda deprecated, use method:'POST' instead. Instead of successuse done like this:

类型:'POST'有点弃用,使用方法:'POST'代替。而不是成功使用这样做:

$.ajax({....... your stuff .......}).done(function(data){
window.location.reload(); //whats the point of using ajax here, if you do a reload then?
})