Ajax从PHP函数获取错误或成功消息
我正在使用JSON post将数据发布到php函数。我希望收到两种不同类型的响应,成功和错误。 例如,以下函数:Ajax从PHP函数获取错误或成功消息,php,jquery,ajax,Php,Jquery,Ajax,我正在使用JSON post将数据发布到php函数。我希望收到两种不同类型的响应,成功和错误。 例如,以下函数: function some_function(){ if ($error){ echo "some error"; exit; } else { echo "everything is ok" exit; } } 在这种情况下,这两条消息都将作为success返回。如何使错误消息返回
function some_function(){
if ($error){
echo "some error";
exit;
} else {
echo "everything is ok"
exit;
}
}
在这种情况下,这两条消息都将作为success返回。如何使错误消息返回到ajax中的error函数?
以下是ajax代码:
jQuery.ajax({
url: '<?php echo $url; ?>',
data: response,
dataType: 'JSON',
type: 'POST',
success:function(data){
console.log(data);
},
error: function(data, errorThrown){
console.log(errorThrown);
}
});
jQuery.ajax({
url:“”,
数据:答复,
数据类型:“JSON”,
键入:“POST”,
成功:功能(数据){
控制台日志(数据);
},
错误:函数(数据、错误抛出){
console.log(错误抛出);
}
});
试试这个:
jQuery.ajax({
url: '<?php echo $url; ?>',
data: response,
dataType: 'JSON',
type: 'POST',
success:function(data){
if(data['error']){
alert(data['error']);
}else{
alert(data['success']);
}
},
error: function(data, errorThrown){
console.log(errorThrown);
}
});
试试这个:
jQuery.ajax({
url: '<?php echo $url; ?>',
data: response,
dataType: 'JSON',
type: 'POST',
success:function(data){
if(data['error']){
alert(data['error']);
}else{
alert(data['success']);
}
},
error: function(data, errorThrown){
console.log(errorThrown);
}
});
如果您想强制它转到错误回调,那么只需强制服务器做出错误响应即可。考虑这个例子:
<?php
$url = 'index.php'; // sample value
function response($value) {
if($value == 'true') {
http_response_code(200);
$data = array('status' => 200, 'message' => 'Server response is okay!');
echo json_encode($data);
} else {
http_response_code(404);
}
exit;
}
if(isset($_POST['response'])) {
$response = $_POST['response'];
response($response);
}
?>
<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
jQuery.ajax({
url: '<?php echo $url; ?>',
data: {response: true}, // <-- set to TRUE or FALSE
dataType: 'JSON',
type: 'POST',
success:function(data){
console.log(data);
},
error: function(data, errorThrown){
console.log('error => ' + errorThrown);
}
});
});
</script>
$(文档).ready(函数(){
jQuery.ajax({
url:“”,
数据:{响应:真},//< p>如果您想强制它返回到错误回调,那么只需强制从服务器发出错误的响应。请考虑这个例子:
<?php
$url = 'index.php'; // sample value
function response($value) {
if($value == 'true') {
http_response_code(200);
$data = array('status' => 200, 'message' => 'Server response is okay!');
echo json_encode($data);
} else {
http_response_code(404);
}
exit;
}
if(isset($_POST['response'])) {
$response = $_POST['response'];
response($response);
}
?>
<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
jQuery.ajax({
url: '<?php echo $url; ?>',
data: {response: true}, // <-- set to TRUE or FALSE
dataType: 'JSON',
type: 'POST',
success:function(data){
console.log(data);
},
error: function(data, errorThrown){
console.log('error => ' + errorThrown);
}
});
});
</script>
$(文档).ready(函数(){
jQuery.ajax({
url:“”,
数据:{response:true},//谢谢。下面的JSON示例运行良好。请查找它
jQuery.ajax({
url: '<?php echo $url; ?>',
data: response,
dataType: 'JSON',
type: 'POST',
success: response_function
});
response_function(response){
if(response.status == 1){
//success response
alert(response.message);
}else{
//failure response
alert(response.message);
return false;
}
}
Elangovan谢谢。下面的JSON示例运行良好。请查找它
jQuery.ajax({
url: '<?php echo $url; ?>',
data: response,
dataType: 'JSON',
type: 'POST',
success: response_function
});
response_function(response){
if(response.status == 1){
//success response
alert(response.message);
}else{
//failure response
alert(response.message);
return false;
}
}
Elangovan我没有足够的声誉发表评论,但从JQuery 1.5开始,您需要使用
error: function(data) {
console.log(data['responseText']);
}
要获取PHP返回的json_编码数组,我没有足够的声誉发表评论,但从JQuery 1.5开始,您需要使用
error: function(data) {
console.log(data['responseText']);
}
要获取PHP返回的json_编码数组。当服务器的响应与预期不符时,将执行错误回调。在您的情况下,您需要的是json。但是,如果响应不是json格式,则将转到错误函数。当服务器的响应与预期不符时,将执行错误回调服务器将不是您所期望的。在您的情况下,您期望的是Json。但是,如果响应不是Json格式,它将转到控制台日志中的错误函数I getUndefined
:(控制台日志中的I getUndefined
:(