在内部发布请求,请单击“侦听器”,而不是重定向到请求的页面



所以在我的html中,我有一个元素

<button">Add to Cart</button>

对于此按钮,请点击功能,我在脚本中的单击"侦听器"中进行操作。我的脚本是

<script type="text/javascript">
window.addEventListener('click',function(e)
        {
            e = e || window.event;
            var target = e.target || e.srcElement;
            console.log("value of the element clicked== "+target.innerHTML);
            if(target.innerHTML=="Add to Cart" || target.innerHTML=="Remove From Cart")
            {
              var uris=target.parentNode.parentNode.querySelector("#uris").value;   
              var username=target.parentNode.parentNode.querySelector("#usernames").value;
              console.log("user name=="+username); 
              console.log(target);
              if(target.innerHTML=="Add to Cart")
              {
                console.log("it is comming here");  
                var productid=target.parentNode.parentNode.querySelector("#proid").innerHTML;
                $.post('AddCart1',{product:productid,username:username,uri:uris}, function () {
                });
              } 
              else
              { 
                var productid=target.parentNode.parentNode.querySelector("#proid").value;
                console.log("parent id=="+productid); 
                $.post('RemoveItem',{product:productid,username:username,uri:uris}, function () {
                });
              }
            }   
        }, false);
</script>

因此,当我单击按钮时,我将获取日志"它在此处进行"。而且,我在数据库中所做的所有更新都在我的数据库中反映出来。但是该页面没有重定向到请求的URL。它保留在同一页面中而无需重新加载。问题是什么?

嗨,我能看到的是您没有做任何重定向页面因此,当您使用$ .post发送请求时,它将从请求的页面返回您的回复,并且您将在同一页面上b。

to redirect you can use following methods
// similar behavior as an HTTP redirect
window.location.replace("your url");
// similar behavior as clicking on a link
window.location.href = "your url";  

您可以在您的$ post请求中使用它在邮政请求的成功中,您可以将其重定向到其他页面,甚至可以在没有$ .post的情况下重定向。

包括jQuery和jquery.redirect.min.js插件,您可以简单地做类似的事情:

$().redirect('your url', {post data in key value pair i.e.'key':'value'});

,我也遇到了这可能起作用!

$.redirectPost("your URL", {post data in key value pair i.e.'key':'value'});

最新更新