Sur*_*rya 5 javascript jquery post
这是我的代码
<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>
Run Code Online (Sandbox Code Playgroud)
编辑
在服务器端它的东西是这样的:
@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
Run Code Online (Sandbox Code Playgroud)
我基本上是服务器端的人,很少写JS.我发送了"错误":如果错误消息和"错误"错误,则为true:如果服务器上没有错误,则为false!
我不知道为什么在上面的代码中条件逻辑不起作用!! 任何人都可以帮我修复它吗?
小智 9
试试这个......
$(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;
});
});
Run Code Online (Sandbox Code Playgroud)
success: function(data)
{
if(data.error){
//alert error
}
else{
//alert('Success');
}
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
alert('Internal server error');
//some stuff on failure
}
Run Code Online (Sandbox Code Playgroud)
此error
回调还处理服务器端未捕获的错误。
归档时间: |
|
查看次数: |
21002 次 |
最近记录: |