使用require时,没有从ajax请求发送响应

时间:2014-11-28 23:39:15

标签: php jquery ajax header require

我有一个php页面,send.php,当我去那个页面时,它成功地回显了一个数组。但是,当我尝试在index.php上使用ajax访问该页面时,没有响应。

这是我访问的页面 send.php

error_reporting(E_ALL);
ini_set('display_errors', 1);
$json = array(
    "test" => null,
    "success" => null,
    "urls" => null,
); 
$json = array("hi" => null, "success" => null);
$class = new test();
$json["success"] = true;
$json["hi"] = $class->_array;   
echo json_encode($json);


class test{
    public $_array = array();
    public $urlPreArray;    
    public $sensoredArray;

    function __construct(){
        require_once('./mysqlDB.php'); //this causes nothing to be sent back
        $cookieString = $_COOKIE['crUrl'];
        $this->_array = explode(",", $cookieString);    
    }
}

这是我的索引页

<html>
<head>
    <script src="jquery1.11.js"></script>
    <meta charset="utf-8">
    <script> 
    function load(){    
        $.get("./php/send.php",
        {
             action:"displayUrlsNotes"
        },
        function(data, status){
            console.log(status);
            console.log(data);
        }, "json");
        console.log("end");
    }
    </script>
</head>
<body>
    <button onclick='load();'>Click Me</button>
</body>
</html>

如果我在我的浏览器中发送send.php它可以工作并打印数组,但是当我转到index.php并按下按钮时,ajax请求成功,但没有发回任何内容。

如果我注释掉require_once('。/ mysqlDB.php'),它确实有效;在send.php中

我是新手在PHP中使用类,并且不确定我需要谷歌找到答案。

2 个答案:

答案 0 :(得分:1)

我明白了。问题是我的mysqlDB.php页面上有html代码。谢谢你告诉我查看我的文件结构。

答案 1 :(得分:0)

我怀疑这与您的文件夹结构有关。

您已使用相对路径来包含数据库文件。使用完整路径是更好的做法,然后您可以从任何上下文调用脚本而没有任何问题。

我会建议像:

require_once($_SERVER['DOCUMENT_ROOT'].'/mysqlDB.php'); 

或者您可以将根定义为公共配置文件中的常量:

define('ROOT_DIR', '/home/mysite/public_html');
require_once(ROOT_DIR.'/mysqlDB.php');
相关问题