AJAX answer not working with PHP require_once

I have a problem with ajax answer. My test php file contents look like this:

<?php $values = array('value'=>'123', 'key'=>'Test' ); echo json_encode($values); ?> 

And everything works fine. But the problem is when I try to include the database connection file, and then the response stops.

Basically, if I add require_once 'database.php'; Connection file contents:

 <?php $login = new PDO("sqlsrv:Server=SERVER;Database=db", "login", "pass"); ?> 

I already tried using

 ini_set('display_errors', 1); error_reporting(E_ALL); 

but nothing. This connection is excellent, the file location is also excellent. And work with another site.

The contents of the test.php file:

 <?php ini_set('display_errors', 1); error_reporting(E_ALL); require_once 'database.php'; $values = array('value'=>'123', 'key'=>'test'); echo json_encode($values); ?> 

database.php file in the right place.

+5
source share
1 answer

Go step by step: -

1.) First of all, you need to run your test.php file directly to make sure there are no errors or warnings.

2.) If the file works and the json echo is fine (without any warnings or errors), only then run the ajax call to get the answer.

3.) You will definitely get an answer. If not, then a problem may arise in your ajax function.

+1
source

Source: https://habr.com/ru/post/1236723/


All Articles