javascript - JsonResponse 和 HTTPResponse 有什么区别?
问题描述
我正在关注一个教程,这个人使用了这两行代码。我试图了解 JsonResponse 和 HttpResponse 之间的区别。
JsonResponse 和 HttpResponse 都会导致用户收到弹出消息 ( $.alert({
)。
当这两种情况下的输出都是一个$.alert({
时,我有点困惑为什么你在一个实例中需要一个 JsonResponse 而在另一个实例中需要一个 HttpResponse 。
if request.is_ajax():
return JsonResponse({"message": "Thank you for your submission"})
if request.is_ajax():
return HttpResponse(errors, status=400, content_type='application/json')
视图.py
if contact_form.is_valid():
print(contact_form.cleaned_data)
if request.is_ajax():
return JsonResponse({"message": "Thank you for your submission"})
if contact_form.errors:
print("there are errors")
errors = contact_form.errors.as_json()
if request.is_ajax():
return HttpResponse(errors, status=400, content_type='application/json')
电子商务.js
displaySubmitting(contactFormSubmitBtn, "", true)
$.ajax({
method: contactFormMethod,
url: contactFormEndpoint,
data: contactFormData,
success: function(data){
contactForm[0].reset()
$.alert({
title: "Success!",
content: data.message,
theme: "modern",
})
setTimeout(function(){
displaySubmitting(contactFormSubmitBtn, contactFormSubmitBtnTxt, false)
}, 500)
},
error: function(error){
console.log(error.responseJSON)
var jsonData = error.responseJSON
var msg = ""
$.each(jsonData, function(key, value){ // key, value array index / object
msg += key + ": " + value[0].message + "<br/>"
})
$.alert({
title: "Oops!",
content: msg,
theme: "modern",
})
这是联系表格
这是您输入正确数据后的联系表格
这是有错误的联系表单(在这种情况下,错误是用户没有输入 Gmail 地址)。
解决方案
推荐阅读
- azure - 我可以使用不同的 appsettings.xxx.json 文件和调试配置文件设置多个开发环境吗?
- git - 基于 Github 上比较的拉取请求摘要或变更日志
- .net - Asp .net 核心 httpsys 异常 - 拒绝访问
- css - CSS 变换就像开门一样
- git - 在 PyCharm IDE 中配置 git 设置
- javascript - 在 $http.get 的 AngularJS 指令中加载模板属性
- flask - Flask 的 SessionInterface 中的 get_cookie_name 之类的东西?
- python - 如何从 csv 列表中获取某些信息?
- r - mutate_at 可以根据包装的函数名称命名变量吗?(dplyr + rlang 问题)
- r - R - 传单地图上的 plotMapDomFlows(“流”包)