I have a php page , send.php that is successfuly echoing out an array when I go to that page. However When i try to use ajax on index.php to access that page there is no response.
This the page I am accessing 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);
}
}
This is my index page
<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>
If I go to send.php in my browser it works and the array is printed, but when I go to index.php and press the button the ajax request is made successfuly but nothing is sent back.
It does work if I comment out require_once('./mysqlDB.php'); in send.php
I am new to using classes in php and wansn't sure what exactly I needed to google to find an answer.
I figured it out. The problem was I had html code on my mysqlDB.php page. Thanks for telling me to look into my file structure.
I suspect this is to do with your folder structure.
You have used a relative path to include your database file. It is better practice to use the full path, then you can call the script from any context without any problem.
I would suggest something like:
require_once($_SERVER['DOCUMENT_ROOT'].'/mysqlDB.php');
or you can define the root as a constant in a common config file:
define('ROOT_DIR', '/home/mysite/public_html');
require_once(ROOT_DIR.'/mysqlDB.php');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.