Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Wordpress ajax调用将返回整个html页面_Javascript_Php_Jquery_Ajax_Wordpress - Fatal编程技术网

Javascript Wordpress ajax调用将返回整个html页面

Javascript Wordpress ajax调用将返回整个html页面,javascript,php,jquery,ajax,wordpress,Javascript,Php,Jquery,Ajax,Wordpress,我在wordpress网站上运行了以下javascript: var PostData = "Action=refresh-cart"; jQuery.ajax({ dataType: "text", type: 'POST', url : location.href, c

我在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>
完成LOL


但是我却收到了一整页的html!?为什么

它将返回在您的url“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>

此处的html内容将不显示

问题的出现是因为location.href是一个高级URL,所以wordpress在ajax请求周围注入了大量html


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

那么实际上,PHP页面上的所有内容都是上面提到的吗?还有什么其他html回来了,或者你能告诉我吗?当我在本地运行时,你的代码似乎能按预期工作,在您的问题中包含的初始化HTML页面的代码之外,是否还有其他代码?php实际上还有很多代码,但都围绕着操作变量上的if语句-所以这应该可以正常工作?好吧,那么问题是,location.href是:所以我认为请求正在通过许多php文件。我怎样才能知道我的特定wordpress插件php文件的URL是什么?但最终,我还是使用了这种方法来实现ajax,因为否则我无法访问wordpress API: