简体   繁体   English

使用require指令不调用Ajax成功函数

[英]Ajax success function isn't called with require instruction

I have really weird problem with AJAX. 我有非常奇怪的AJAX问题。 This is fragment of file with js code and post method which sends params to php file via AJAX: 这是带有js代码和post方法的文件片段,它通过AJAX将params发送到php文件:

 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代码的片段(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. 当我使用require指令评论或删除该行时,一切正常,但当它处于活动状态时,不会触发成功函数。

  1. JS post method sends correct params. JS post方法发送正确的params。
  2. Php file receives it, does proper operations and returns correct data to js script (I can see it in FireBug). Php文件接收它,执行正确的操作并将正确的数据返回给js脚本(我可以在FireBug中看到它)。
  3. Success function isn't fired. 成功函数未被触发。

Why instruction which isn't related with AJAX, causes this problem? 为什么与AJAX无关的指令会导致这个问题?

Edit. 编辑。

functions.php: 的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. EDIT2。

There is no errors and when I paste functions from functions.php to index.php everything works fine. 没有错误,当我将函数从functions.php粘贴到index.php时,一切正常。 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 . 这可能是functions.php一个错误。

EDIT: If you haven't set display_errors = On in your php.ini file, use these lines in your code: 编辑:如果你没有在php.ini文件中设置display_errors = On ,请在代码中使用以下行:

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). 确保使用的是检查语法的IDE(例如,Netbeans)。

  2. Separate the file into two, like so: 将文件分成两部分,如下所示:

index.php 的index.php

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

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. 在浏览器窗口中运行它,这应该可以让您了解您正在处理的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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