我运行一个ajax请求tQuery调用一个php函数,每次调用它都有效,我不是说编写的代码应该工作,因为它是正确的,但它的工作原理是因为我可以看到数据库中的输出。但是request.fail(function(jqXHR, textStatus){alert('AJAX Error: '+ 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函数(对不起该演示文稿,我把这只是为了获取信息,因为它工作的exe):
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'));
}
该函数调用另一个PHP函数(retrive_avaible_operator
)retrun
一个值,我想知道这是不是问题,请问AJAX / PHP是否将此信息作为最终输出?祝贺提前
答案 0 :(得分:0)
我的上帝是很多嵌套的ifs。您是否考虑过使用名为例外的内容来生成错误?
我无法检查PHP脚本的返回值。但是,jQuery需要一个json返回(因为你将dataType设置为),如果它得到任何其他内容将会失败...包括一个空字符串。