繁体   English   中英

Wordpress Ajax调用返回HTML的整个页面

[英]Wordpress ajax call is returning an entire page of html

我在我的wordpress网站上运行了以下javascript:

var PostData = "Action=refresh-cart";
                     jQuery.ajax({
                       dataType: "text",
                       type: 'POST',
                       url : location.href,
                       cache: false,
                       data : PostData,
                       complete : function() {  },
                       success: function(data) {
                         //   jQuery("#loading-img").hide();

                           // jQuery("#join-class-div-3").html(data);

                        }           
                });

的PHP是:

<?php 

  if(isset($_POST['Action'])) {
        $Action = $_POST['Action'];
        if($Action == "refresh-cart") {


           echo '<div id="stuff"><p> done LOL </p></div>'; 
            }
        }

    ?>

所以我希望能被寄回:

<div id="stuff"><p> done LOL </p></div>

但是相反,我收到了整个html页面! 为什么!?

它将返回呈现在您的网址“ location.href”中的页面的所有内容

尝试在您的php代码中添加exit()以在回显后停止它。

<?php 
if(isset($_POST['Action'])) {
    $Action = $_POST['Action'];
    if($Action == "refresh-cart") {
         echo '<div id="stuff"><p> done LOL </p></div>';
         exit;
    }
}
?>
<div>html content here will not be displayed</div>

发生问题是因为location.href是一个高级URL,所以wordpress在ajax请求周围注入了html负载。

通过将location.url更改为对wordpress PHP API一部分的plugins.url()的调用plugins.url()调用直接进入我的PHP页面,并且得到正确的响应:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM