|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
为什么不能顺利的报错?返回JSON格式那两句代码是不是写错了?
- @csrf_exempt
- def task_add(request):
- print(request.POST)
- # {'level': ['2'], 'title': ['得到的答复'], 'detail': ['静安分局附件'], 'user': ['2']}
- # 1.用户发过来的数据进行校验
- form = TaskModelForm(data=request.POST)
- if form.is_valid():
- form.save()
- # 不能返回redict,还是要返回JSON格式
- data_dict = {"status": True}
- return JsonResponse(data_dict)
- # print(type(form.errors.as_json()))
- # from django.forms.utils import ErrorDict
- data_dict = {"status": False, 'error': form.errors}
- return JsonResponse(data_dict, ensure_ascii=False)
复制代码
前端页面:
- {% extends 'layout.html' %}
- {% block content %}
- <div class="container">
- <div class="panel panel-default">
- <div class="panel-heading">表单</div>
- <div class="panel-body">
- <form id="formAdd">
- <div class="clearfix">
- {% for field in form %}
- <div class="col-xs-6">
- <div class="form-group" style="position: relative;margin-bottom: 20px;">
- <label>{{ field.label }}</label>
- {{ field }}
- <span class="error-msg" style="color: red;position: absolute"></span>
- </div>
- </div>
- {% endfor %}
- <div class="col-xs-12">
- <button id="btnAdd" type="button" class="btn btn-primary">提 交</button>
- </div>
- </div>
- </form>
- </div>
- </div>
- <hr/>
- <h1>Ajax学习</h1>
- <h3>示例1</h3>
- <input id="btn1" type="button" class="btn btn-primary" value="点击1" alt="">
- <h3>示例2</h3>
- <input type="text" id="txtUser" placeholder="姓名"/>
- <input type="text" id="txtAge" placeholder="年龄"/>
- <input id="btn2" type="button" class="btn btn-primary" value="点击2"/>
- <h3>示例3</h3>
- <form id="form3">
- <input type="text" name="name" placeholder="姓名"/>
- <input type="text" name="age" placeholder="年龄"/>
- <input type="text" name="email" placeholder="邮箱"/>
- <input type="text" name="more" placeholder="介绍"/>
- </form>
- <input id="btn3" type="button" class="btn btn-primary" value="点击3"/>
- </div>
- {% endblock %}
- {% block js %}
- <script type="text/javascript">
- $(function () {
- // 页面框架加载完成之后代码自动执行
- bindBtn1Event();
- bindBtn2Event();
- bindBtn3Event();
- bindBtnAddEvent();
- })
- function bindBtn1Event() {
- $('#btn1').click(function () {
- $.ajax({
- url: '/task/ajax/',
- type: 'post',
- data: {
- ni: 123,
- n2: 456
- },
- dataType: "JSON",
- {#将字符串res转化为前端对象#}
- success: function (res) {
- console.log(res);
- console.log(res.status);
- console.log(res.data);
- }
- })
- })
- }
- function bindBtn2Event() {
- $('#btn2').click(function () {
- $.ajax({
- url: '/task/ajax/',
- type: 'post',
- data: {
- name: $("#txtUser").val(),
- age: $("#txtAge").val()
- },
- dataType: "JSON",
- {#将字符串res转化为前端对象#}
- success: function (res) {
- console.log(res);
- console.log(res.status);
- console.log(res.data);
- }
- })
- })
- }
- function bindBtn3Event() {
- $('#btn3').click(function () {
- $.ajax({
- url: '/task/ajax/',
- type: 'post',
- data: $("#form3").serialize(),
- dataType: "JSON",
- {#将字符串res转化为前端对象#}
- success: function (res) {
- console.log(res);
- console.log(res.status);
- console.log(res.data);
- }
- })
- })
- }
- function bindBtnAddEvent() {
- $('#btnAdd').click(function () {
- $(".error-msg").empty();
- $.ajax({
- url: '/task/add/',
- type: 'post',
- data: $("#formAdd").serialize(),
- dataType: "JSON",
- {#将字符串res转化为前端对象#}
- success: function (res) {
- if (res.status) {
- alert("添加成功");
- } else {
- {#console.log(res.error)#}
- $.each(res.error, function (name, data) {
- {#console.log(name,data);#}
- $("#id_" + name).next().text(data[0]);
- })
- }
- }
- })
- })
- }
- </script>
- {% endblock %}
复制代码
ensure_ascii=False 是 json.dumps() 方法的参数,用于确保非 ASCII 字符正确输出。如果你在直接调用 json.dumps() 时使用它,你可以得到期望的效果。
然而,Django 的 JsonResponse 并不直接接受这个参数。它有一个参数 json_dumps_params ,你可以通过这个参数将 ensure_ascii=False 传递给内部的 json.dumps() 方法。
你可以像这样做:
- return JsonResponse(data_dict, json_dumps_params={'ensure_ascii': False})
复制代码
这样, JsonResponse 将正确处理非 ASCII 字符,避免出现乱码问题。你也可以通过 json_dumps_params 参数传递任何其他 json.dumps() 接受的参数。
|
|