Jquery Ajax 发布成功/错误功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7439606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery Ajax post success/error function
提问by Matt
I have a post function that is not being caught on error.
我有一个没有被错误捕获的 post 函数。
Here's the value being returned from postride.php.
这是从 postride.php 返回的值。
if ($group_id==0) {
echo 'No group selected';
return false;
exit;
}
Here's the jquery code:
这是jquery代码:
$(document).ready(function(){
$('#postride').submit(function(event) {
event.preventDefault();
dataString = $("#postride").serialize();
$.ajax({
type: "post",
url: "postride.php",
data:dataString,
error: function(returnval) {
$(".message").text(returnval + " failure");
$(".message").fadeIn("slow");
$(".message").delay(2000).fadeOut(1000);
},
success: function (returnval) {
$(".message").text(returnval + " success");
$(".message").fadeIn("slow");
$(".message").delay(2000).fadeOut(1000);
//setTimeout( function() { top.location.href="view.php" }, 3000 );
}
})
return false;
});
});
});
The post function returns false, but the error function does not fire, only the success function. It will post "No group selected success".
post 函数返回false,但error 函数不会触发,只有success 函数。它将发布“没有组选择成功”。
Thanks for any help!
谢谢你的帮助!
回答by Anthony Hyman
The error option in jQuery ajax methods is for errors caused by a bad connection, timeout, invalid url, things of that nature. It's not the kind of error that you're thinking.
jQuery ajax 方法中的错误选项用于由错误连接、超时、无效 url 等性质的事情引起的错误。这不是你想的那种错误。
What most people do is something like this...
大多数人做的事情是这样的......
php
php
if ($group_id == 0) {
echo json_encode(array(
'status' => 'error',
'message'=> 'error message'
));
}
else
{
echo json_encode(array(
'status' => 'success',
'message'=> 'success message'
));
}
javascript
javascript
$(document).ready(function(){
$('#postride').submit(function(event) {
event.preventDefault();
dataString = $("#postride").serialize();
$.ajax({
type: "post",
url: "postride.php",
dataType:"json",
data: dataString,
success: function (response) {
if(response.status === "success") {
// do something with response.message or whatever other data on success
} else if(response.status === "error") {
// do something with response.message or whatever other data on error
}
}
})
return false;
});
回答by Philip Schweiger
wsanville is correct: "success" or "failure" refers to the success/failure of the ajax request.
wsanville 是正确的:“成功”或“失败”是指 ajax 请求的成功/失败。
Rather than changing up the HTTP header, though, I find it useful to return information in a json response. So for instance, I would have postride.php respond like so:
不过,我发现在 json 响应中返回信息很有用,而不是更改 HTTP 标头。例如,我会让 postride.php 像这样响应:
$error = false;
if ($group_id==0) {
$error = 'No group selected';
}
$response = array('error'=>$error);
echo json_encode($response);
Then in the "success" callback of the JS, I would do this:
然后在 JS 的“成功”回调中,我会这样做:
success: function (returnval) {
if (returnval.error) {
$(".message").text(returnval + " failure")
.fadeIn("slow",function(){
$(this).delay(2000).fadeOut(1000);
});
} else {
//do whatever user should see for succcess
}
回答by wsanville
The error
method of jQuery's ajax
function will only get called when the page requested returns an error status code. Your example will probably return HTTP 200, which is why success
gets called; jQuery does not interrogate the contents of a response to determine whether or not it was successful.
error
jQueryajax
函数的方法只会在请求的页面返回错误状态代码时被调用。您的示例可能会返回 HTTP 200,这就是success
被调用的原因;jQuery 不会询问响应的内容来确定它是否成功。
You should set a 4xx status codewhen $group_id
is 0, by replacing your echo
call with a call to header.