简体   繁体   中英

Ajax success function isn't called with require instruction

I have really weird problem with AJAX. This is fragment of file with js code and post method which sends params to php file via AJAX:

 var params = $('#add').serializeArray(); $.post('code/bg/adding_c.php', params, function(ret) { //body of success function }, 'json'); 

And this is fragment of php code (adding_c.php):

 <?php require "functions.php"; //irrelevant operations $return = array( 'status' => $status, 'msg' => $msg, 'id' => $id ); echo json_encode($return); ?> 

Everything works when I comment or delete the line with require instruction but when it's active, success function isn't fired.

  1. JS post method sends correct params.
  2. Php file receives it, does proper operations and returns correct data to js script (I can see it in FireBug).
  3. Success function isn't fired.

Why instruction which isn't related with AJAX, causes this problem?

Edit.

functions.php:

 <?php $host = 'localhost'; $user = 'root'; $pass = ''; $db = 'dbname'; $conn = new mysqli($host, $user, $pass, $db); function querySQL($query) { global $conn; $result = $conn->query($query); return $result; } function cleanSQL($conn, $string) { return htmlentities(fixSQL($conn, $string), ENT_COMPAT, 'UTF-8'); } function fixSQL($conn, $string) { if(get_magic_quotes_gpc()) $string = stripslashes($string); return $conn->real_escape_string($string); } function fPassword($pass) { $salt1 = 'salt1'; $salt2 = 'salt2'; $token = hash('ripemd128', "$salt1$pass$salt2"); return $token; } ?> 

Edit2.

There is no errors and when I paste functions from functions.php to index.php everything works fine. I don't know what to do now. It seems that require word is a problem here. I can't add these functions to every file in which I need them.

It is probably an error within functions.php .

EDIT: If you haven't set display_errors = On in your php.ini file, use these lines in your code:

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

Also, are you sure it is not a parsing/syntax error? If that is the case, there's a few things you will want to do:

  1. Make sure you are using an IDE that checks syntax (Netbeans, for example).

  2. Separate the file into two, like so:

index.php

<?php
    ini_set("display_errors", "1");
    error_reporting(E_ALL);
    include 'error.php';

error.php

require "functions.php";

//irrelevant operations

$return = array(
    'status' => $status,
    'msg' => $msg,
    'id' => $id
);

echo json_encode($return);

Run this in a browser window and that should give you an idea of what you are dealing with.

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