所以我在我的javascript文件中有这个功能:
function UpdateThisCourseHistory(){
var ajax= new Ajax.Request("runNeededQueries.php",
{
method: "POST",
parameters: {database: "history", action:"update"},
onSuccess: function(){alert("This Course History Entry Has Been Updated");},
onFailure: function(){alert("Could Not Find The Entry With The Specified Primary Key");}
}
);
}
假设这是我的php文件
<?php
header('HTTP/1.0 400 Bad Request');
?>
这会使onFailure方法执行吗?
答案 0 :(得分:3)
当PHP脚本使用HTTP 2xx响应代码响应时,将触发onSuccess回调,如果返回HTTP错误代码,则会触发onFailure回调。如果您想根据响应值采取行动,则必须选择:
header('HTTP/1.0 400 Bad Request');
onSuccess: function(transport){ if transport.responseText == 'expected value' ...}
答案 1 :(得分:1)
使您的PHP脚本输出一些内容,最好是JSON。
像:
{ action: 'find_id', status: true }
从响应中获取数据,以便查看操作是否成功。
function(data){
if (data.status){
alert("Everything is OK") ;
} else {
alert("Failed") ;
}
}