Django 返回json

view.py

import json
from django.http import HttpResponse
class AddAskView(View):
    def post(self,request):
            resp = {'status': 'success', 'detail123': '我喜欢季慧新'}
            return HttpResponse(json.dumps(resp), content_type="application/json")
       

html

$(function(){
        $('#jsStayBtn').on('click', function(){

            $.ajax({
                cache: false,
                type: "POST",
                url:"/ora/add_ask/",
                data:$('#jsStayForm').serialize(),
                async: true,
                success: function(data) {
                    console.log(data.detail123)
                     console.log(data.status)
                    if(data.status == 'success'){
                        $('#jsStayForm')[0].reset();
                        alert("提交成功")
                    }else if(data.status == 'fail'){
                        $('#jsCompanyTips').html(data.msg)
                    }
                },

            });
        });
    })

HttpResponse(json.dumps(resp), content_type="application/json")的json数据被success: function(data)中的data接收。

你可能感兴趣的:(Django 返回json)