无法在 Ajax 中的错误函数中显示错误消息 - Django



我试图显示错误消息从服务器到客户端网站使用ajax,但它不会工作,当我使错误消息的功能,

#other codes for saving the post,there is not error
success_meesage = f'success'
return JsonResponse({'success':'success','success_msg':success_meesage})
else:
error_message=f'there is an error into your two dates please make sure check in smaller than check out'
return JsonResponse({'success':False,'error_taken':True,'error_message':error_message})

ajax代码

const form = document.getElementById('post-form')
form.addEventListener("submit",submitHanler);
function submitHanler(e){
e.preventDefault();
$.ajax({
type:'POST',
url:"my-url",
data:$("#post-form").serialize(),
dataType:'json',
success:successFunction,
error:errorFunction,

})        
}
function successFunction(data){
// console.log(data)
if(data.success='success' && data.success_msg){
form.reset();
alertify.success(data.success_msg)
}
}
function errorFunction(request, status, error){
console.log(request.responseText)

if(error.error_taken == true){
alertify.alert('warning !','message')
alertify.alert(error.error_message,function(){
});
}

}
}
我也试过这个:function errorFunction(jqXHR, exception)它也没有显示任何东西,我尝试了很多方法,但都不起作用!提前谢谢你……

您返回的是JsonResponse,但状态码为200,因此,根据状态码,这意味着请求成功。

因此,我们可以返回一个状态码在200-399范围之外的JsonResponse,例如一个400的HTTP响应:
if some_condition:
success_meesage = f'success'
return JsonResponse(
{'success':'success','success_msg':success_meesage}
)
else:
error_message=f'there is an error into your two dates please make sure check in smaller than check out'
return JsonResponse(
{'success':False,'error_taken':True,'error_message':error_message},
status=400
)

相关内容

  • 没有找到相关文章

最新更新