I've the SET the following AJAX request. The url is actually a file called "Controller.php", and from this it can be understood that, it is a MVCish styled project.
$.ajax({
url: "http://127.0.0.1/Controller/fetch",
method: "POST",
data: {
function: 'fetch'
},
success: function(data){ var string = JSON.stringify(data); var json = JSON.parse(string);
$("#div#id").html(json);
}
});
This is the code in the "Controller.php":
<?php
class Controller extends Base_controller{
public function fetch(){
if(isset($_POST['function'])){
// Execute a SQL Query and return data
echo json_encode($result_of_the_query);
}
}
}
The request works perfectly and fetches the requested data, but also brings some unwanted HTML code from a page (index.php - everything is routed from here), as shown below: I tried echo strip_tags(json_encode($result_of_the_query));
, as mentioned here AJAX returns HTML code with output. But it didn't work. What modification should I do in my code??
<!DOCTYPE html> <!-- <script src="http://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script> --> <script src="/vector/js/jquery-3.5.0.min.js"></script> [{"column_one":"14-Jun-2020","column_two":null,"column_three":null},{"column_one":"07-Jun-2020","column_two":null,"column_three":null}]
My bootstrap.php file is very similar to:
// Load Config (database)
require_once 'config/config.php';
// Load Helpers
require_once 'helpers/url_helper.php';
require_once 'helpers/session_helper.php';
// Autoload Core Libraries
spl_autoload_register(function($className){
require_once 'libraries/' . $className . '.php';
});