简体   繁体   English

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

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

I have the following piece of javascript which runs in my wordpress site : 我在我的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);

                        }           
                });

The php is : 的PHP是:

<?php 

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


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

    ?>

So I expect to be sent back : 所以我希望能被寄回:

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

But instead I receive an entire page of html!? 但是相反,我收到了整个html页面! Why!? 为什么!?

It will return all contents from the page rendered at your url "location.href" 它将返回呈现在您的网址“ location.href”中的页面的所有内容

Try adding a exit() to your php code to stop it after your echo. 尝试在您的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>

The problem was occuring because location.href was a high level URL, so wordpress was injecting loads of html around the ajax request. 发生问题是因为location.href是一个高级URL,所以wordpress在ajax请求周围注入了html负载。

Through altering location.url to a call to plugins.url() which is part of the wordpress PHP API - the AJAX call goes directly to my PHP page and I get the correct response :) 通过将location.url更改为对wordpress PHP API一部分的plugins.url()的调用plugins.url()调用直接进入我的PHP页面,并且得到正确的响应:)

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

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