ajax调用django写好的api

django构建API

model.py文件

  • 这里不做展示

url.py文件

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.dashboard, name='show_dashboard'),
    url(r'^show_content$', views.show_dashboard, name='show_content'),
]

views.py文件

from django.core import serializers
from django.shortcuts import render_to_response
from django.http import JsonResponse
from .models import controlInfo

def show_dashboard(request):
    data = serializers.serialize("json", controlInfo.objects.all())
    return JsonResponse(data, safe=False)

结果数据为
这里写图片描述

html

    <script>
    $.ajax({
       method: 'GET',
       url: "show_content" ,
       dataType: 'json' ,
       success: function(data){
            console.log(data);
            var s = data[0].fields;
            console.log(s);
            $("#result").html(s.title);
       }
    });
    script>

你可能感兴趣的:(ajax调用django写好的api)