jQuery 如何在Jquery Ajax Form提交中添加错误处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17313148/
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
How to add error handling in Jquery Ajax Form submission
提问by Surya
This is my code
这是我的代码
<script type="text/javascript">
$(document).ready(function() {
$('#spc-comment-flag-form').submit(function() {
$.ajax({
data: $(this).serialize(),
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(data) {
if( data['error'] == false) {
var msg = 'We got your flag. Our moderators will now look into it. You may close the window now!';
$('#spc-comment-flag-response').html(msg);
}
else {
$('#spc-comment-flag-response').html(data);
}
},
});
return false;
});
});
</script>
edit
编辑
on server side its something like this:
在服务器端它是这样的:
@csrf_protect
@login_required
def flag(request, comment_id, next=None):
if not request.is_ajax():
raise Http404
data = {}
if request.method == 'POST':
...
data = simplejson.dumps(data)
return HttpResponse(data, mimetype="application/javascript")
else:
raise Http404
I am basically server side guy and seldom have to write JS. I sent "error" : true if there is an error with error message and "error" : false if no error on server!
我基本上是服务器端的人,很少需要编写 JS。我发送 "error" : true 如果有错误消息和 "error" : false 如果服务器上没有错误!
I don't know why in the above code the conditional logic is not working!! Can anyone help me fix it?
我不知道为什么在上面的代码中条件逻辑不起作用!!任何人都可以帮我修复它吗?
回答by Tejas Savaliya
try this one...
试试这个...
$(document).ready(function() {
$('#spc-comment-flag-form').submit(function() {
$.ajax({
data: $(this).serialize(),
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(data) {
if( data['error'] == false) {
var msg = 'We got your flag. Our moderators will now look into it. You may close the window now!';
$('#spc-comment-flag-response').html(msg);
}
else {
$('#spc-comment-flag-response').html(data);
}
},
error: function (data) {
var r = jQuery.parseJSON(data.responseText);
alert("Message: " + r.Message);
alert("StackTrace: " + r.StackTrace);
alert("ExceptionType: " + r.ExceptionType);
}
});
return false;
});
});
回答by Premshankar Tiwari
you have to use
你必须使用
error: function( jqXHR jqXHR, String textStatus, String errorThrown){
...
...
}
回答by Olrac
You may use this code.. if you want to handle the error in web request...
您可以使用此代码.. 如果您想处理 Web请求中的错误...
http://api.jquery.com/jQuery.ajax/
http://api.jquery.com/jQuery.ajax/
$.ajax({
}).done(function (data) {
}).fail(function (jqXHR, textStatus) {
});
And about in your server side validation you could return the error using json...
在您的服务器端验证中,您可以使用json返回错误...
回答by Paritosh
success: function(data)
{
if(data.error){
//alert error
}
else{
//alert('Success');
}
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
alert('Internal server error');
//some stuff on failure
}
This error
callback also handles the error, which are not caught at the server side.
此error
回调还处理服务器端未捕获的错误。