简体   繁体   中英

AJAX response not working with PHP require_once

I have issue with ajax response. My test php file content looks like:

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

And everything works perfectly. But problem is when I try but include database connection file then response stops.

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

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

I already tried with

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

but nothing. This connection is fine, file location is fine as well. And working with another website.

test.php file content:

<?php
  ini_set('display_errors', 1);
    error_reporting(E_ALL);

 require_once 'database.php';

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

file database.php in right location.

Please go step by step:-

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

2.) If the file runs and echos the json data perfectly(without any warnings or errors), only then run the ajax call to get the response.

3.) You will certainly get the response.if not,then the problem might in your ajax function.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM