Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AJAX在请求时返回错误,但php函数可以工作_Php_Jquery - Fatal编程技术网

AJAX在请求时返回错误,但php函数可以工作

AJAX在请求时返回错误,但php函数可以工作,php,jquery,Php,Jquery,我运行了一个ajax请求trought jQuery,它调用了一个php函数,每次调用该函数都可以工作,我不是说编写的代码应该可以工作,因为它是正确的,但它可以工作,因为我可以在数据库中看到输出。但是request.fail(函数(jqXHR,textStatus){alert('AJAX错误:'+textStatus);})返回错误(parseerror),这是我的代码: $('#createtk').click(function(){ var tit=$('#

我运行了一个ajax请求trought jQuery,它调用了一个php函数,每次调用该函数都可以工作,我不是说编写的代码应该可以工作,因为它是正确的,但它可以工作,因为我可以在数据库中看到输出。但是
request.fail(函数(jqXHR,textStatus){alert('AJAX错误:'+textStatus);})返回错误(
parseerror
),这是我的代码:

$('#createtk').click(function(){
                var tit=$('#title').val();
                var prio=$('#priority').val();
                var wsurl=$('#wsurl').val();
                var dep=$('#dep').val();
                var message=CKEDITOR.instances.message.getData().replace(/\s+/g,' ');
                if(tit.replace(/\s+/g,'')!='' && prio.replace(/\s+/g,'')!='' && dep.replace(/\s+/g,'')!='' && wsurl.replace(/\s+/g,'')!='' && message.replace(/\s+/g,'')!=''){
                    var request= $.ajax({
                        type: 'POST',
                        url: '../php/function.php',
                        data: {act:'create_ticket',tit:tit,prio:prio,dep:dep,wsurl:wsurl,contp:$('#contype').val(),ftpus:$('#ftpus').val(),ftppass:$('#ftppass').val(),message:message},
                        dataType : 'json',
                        success : function (data) {
                            alert('1');
                            if(data[0]=='Created'){
                                alert('2');
                                window.location = "<?php echo dirname(curPageURL()); ?>";

                            }
                            else
                                alert(data[0]);
                        }
                    });
                    request.fail(function(jqXHR, textStatus){alert('AJAX Error: '+ textStatus);});
                }
                else
                    alert('Form Error - Empty Field');
            });

该函数调用另一个php函数(
retrive\u available\u operator
),该函数将
retrun
作为一个值,我想知道这是否是问题所在,AJAX/php是否将此信息详细阐述为最终输出?
提前感谢

我的上帝,这是大量嵌套的ifs。您是否考虑过使用名为异常的方法来生成错误


我无法检查PHP脚本的返回值。但是,jQuery需要一个json返回(当您将数据类型设置为该数据类型时),如果它得到其他任何东西,包括一个空字符串,它将失败。

15
if's
。我的头疼!;)在您的案例中发生了一个解析错误,因为jQuery需要返回一个有效的JSON字符串,但实际情况并非如此。检查您的请求以查看返回的内容。哇。。。。。这是什么?@无情总共有22个
if
。再数一数,没什么值得骄傲的。如果我必须保持这样的状态,我会亲自关门。对于@Dheed,如果未来的数据不是json,ajax将失败,即使requet在控制台中显示
200OK
。它到达最终的if-execute(我尝试创建一个文件,它工作了),所以问题应该是
echo-json_-encode(数组(0=>'Created')
,但如果我没有错的话,它是一个arrayI也可以发布其他函数的函数,把它放在比这更糟糕的位置,但是最后它应该抛出
return$selopid
返回mysqli\u stmt\u错误($stmt)
@Dheed:打开网络控制台,找到呼叫,并准确粘贴响应正文。如果不是JSON,那就是你的问题所在!它返回:
[“Created”][56]
,第一个是回显,第二个是第二个调用函数的返回
else if(isset($_POST['act']) && isset($_SESSION['name']) && $_POST['act']=='create_ticket'){ //controllare

$message=(preg_replace('/\s+/','',$_POST['message'])!='')? htmlentities(preg_replace('/\s+/',' ',$_POST['message']),ENT_QUOTES,'UTF-8'):exit();
$tit=(preg_replace('/\s+/','',$_POST['tit'])!='')? htmlentities(preg_replace('/\s+/',' ',$_POST['tit']),ENT_QUOTES,'UTF-8'):exit();
$dep=(is_numeric($_POST['dep']))? (int)$_POST['dep']:exit();

$prio=(is_numeric($_POST['prio']))? $_POST['prio']:exit();

$wsurl=(preg_replace('/\s+/','',$_POST['wsurl'])!='')? htmlentities(preg_replace('/\s+/',' ',$_POST['wsurl']),ENT_QUOTES,'UTF-8'):exit();
$contype=(is_numeric($_POST['contp']))? (int)$_POST['contp']:exit();
$ftppass=(preg_replace('/\s+/','',$_POST['ftppass'])!='')? htmlentities(preg_replace('/\s+/',' ',$_POST['ftppass']),ENT_QUOTES,'UTF-8'):'';
$ftpus=(preg_replace('/\s+/','',$_POST['ftpus'])!='')? htmlentities(preg_replace('/\s+/',' ',$_POST['ftpus']),ENT_QUOTES,'UTF-8'):'';

if(preg_replace('/\s+/','',$_POST['message'])!=''){
    $mysqli = new mysqli($Hostname, $Username, $Password, $DatabaseName);
    $stmt = $mysqli->stmt_init();
    if($stmt){
        $query = "INSERT INTO ".$SupportTicketsTable."(`department_id`,`user_id`,`title`,`priority`,`website`,`contype`,`ftp_user`,`ftp_password`,`created_time`,`last_reply`) VALUES (?,?,?,?,?,?,?,?,?,?)";
        $prepared = $stmt->prepare($query);
        if($prepared){
            $date=date("Y-m-d H:i:s");
            if($stmt->bind_param('iisissssss', $dep,$_SESSION['id'],$tit,$prio,$wsurl,$contype,$ftpus,$ftppass,$date,$date)){
                if($stmt->execute()){
                    $tkid=$stmt->insert_id;
                    $ip=retrive_ip();
                    $refid=uniqid(hash('sha256',$tkid.$tit),true);
                    $query = "UPDATE ".$SupportTicketsTable." SET enc_id=? WHERE id=? ";
                    if($prepared = $stmt->prepare($query)){
                        if($stmt->bind_param('si', $refid,$tkid)){
                            if($stmt->execute()){
                                $query = "INSERT INTO ".$SupportMessagesTable."(`user_id`,`message`,`ticket_id`,`ip_address`,`created_time`) VALUES (?,?,?,?,?);";
                                if($prepared = $stmt->prepare($query)){
                                    if($stmt->bind_param('isiss', $_SESSION['id'],$message,$tkid,$ip,$date)){
                                        if($stmt->execute()){
                                            $selopid=retrive_avaible_operator($Hostname, $Username, $Password, $DatabaseName, $SupportUserPerDepaTable, $SupportUserTable, $dep);
                                            if(is_numeric($selopid)){

                                                $query = "UPDATE ".$SupportTicketsTable." a ,".$SupportUserTable." b SET a.operator_id=?,a.ticket_status='1',b.assigned_tickets (b.assigned_tickets+1) WHERE a.id=? AND b.id=? ";
                                                if($prepared = $stmt->prepare($query)){
                                                    if($stmt->bind_param('iii', $selopid,$tkid,$selopid)){
                                                        if($stmt->execute()){
                                                            echo json_encode(array(0=>'Created'));
                                                        }
                                                        else
                                                            echo json_encode(array(0=>mysqli_stmt_error($stmt)));
                                                    }
                                                    else
                                                        echo json_encode(array(0=>mysqli_stmt_error($stmt)));
                                                }
                                                else
                                                    echo json_encode(array(0=>mysqli_stmt_error($stmt)));
                                            }
                                            echo json_encode(array(0=>$selopid));
                                        }
                                        else
                                            echo json_encode(array(0=>mysqli_stmt_error($stmt)));
                                    }
                                    else
                                        echo json_encode(array(0=>mysqli_stmt_error($stmt)));
                                }
                                else
                                    echo json_encode(array(0=>mysqli_stmt_error($stmt)));
                            }
                            else
                                echo json_encode(array(0=>mysqli_stmt_error($stmt)));
                        }
                        else
                            echo json_encode(array(0=>mysqli_stmt_error($stmt)));
                    }
                    else
                        echo json_encode(array(0=>mysqli_stmt_error($stmt)));
                }
                else
                    echo json_encode(array(0=>mysqli_stmt_error($stmt)));
            }
            else
                echo json_encode(array(0=>mysqli_stmt_error($stmt)));
        }
        else
            echo json_encode(array(0=>mysqli_stmt_error($stmt)));
    }
    else
        echo json_encode(array(0=>mysqli_stmt_error($stmt)));
    $mysqli->close();
}
else
    echo json_encode(array(0=>'Empty Message'));
}