PHP/AJAX只在回显时工作

PHP/AJAX only works when something gets echoed?

本文关键字:工作 AJAX PHP      更新时间:2023-09-26

我试图将Wordpress博客头包含在php文件中,以使用它作为AJAX调用函数。

define('WP_USE_THEMES',false);
echo 'Something';
require(explode("wp-content",realpath(dirname(__FILE__)))[0].'wp-blog-header.php');

原始代码片段见于:Wordpress include("../../../wp-blog-header");

该代码仅在definerequire函数之间有回显或打印时有效。如果没有它,服务器将返回404错误。

此行为仅在我通过以下AJAX请求瞄准php时发生。

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        document.getElementById('directory_results').innerHTML = xmlhttp.responseText;
    }
}
xmlhttp.open("POST", "http://localhost:8888/appsconnected/wp-content/themes/appsconnected/ajax-loop.php");
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send("platform=" + platforms + "&category=" + category + "&orderby=" + order);

是什么导致了这种行为?

试试这个:

<?php
    define('WP_USE_THEMES',false);
    require(explode("wp-content",realpath(dirname(__FILE__)))[0].'wp-blog-header.php');
?>