zoukankan      html  css  js  c++  java
  • 8.ajax与django后台json数据的交互

    1新建django项目名为json_ajax,应用名为app,在templates模板中新建ajax.html文件

    ajax.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>ajax</title>
        {% load staticfiles %}
        <script type="text/javascript" src="{% static 'js/jquery-3.3.1.min.js' %}"></script>
    </head>
    <body>
    <button id="send">发送</button>
    <script>
        $("#send").click(function () {
            {#json数据#}
            var post_data={
                "name":"weihu",
            };
    
            $.ajax({
                url:"http://127.0.0.1:8000/ajax",
                type:"POST",
                {#发送json数据到服务器#}
                data:post_data,
    
                {#请求成功回调函数#}
                success:function (data) {
                    alert(data)
                    alert("请求成功")
                },
                {#请求失败回调函数#}
                error:function () {
                    alert("服务器请求超时,请重试!")
                }
    
            });
        });
    </script>
    </body>
    </html>

     

    2.在settings配置文件中,注释

     # 'django.middleware.csrf.CsrfViewMiddleware',
    
    STATIC_URL = '/static/'
    
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, 'static'),
    ]

    3.在urls.py文件中,配置path路径

    """json_ajax URL Configuration
    
    The `urlpatterns` list routes URLs to views. For more information please see:
        https://docs.djangoproject.com/en/2.0/topics/http/urls/
    Examples:
    Function views
        1. Add an import:  from my_app import views
        2. Add a URL to urlpatterns:  path('', views.home, name='home')
    Class-based views
        1. Add an import:  from other_app.views import Home
        2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
    Including another URLconf
        1. Import the include() function: from django.urls import include, path
        2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
    """
    from django.contrib import admin
    from django.urls import path
    from app import views
    
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('test',views.test),
        path('ajax',views.ajax),
    ]

    4.在views.py中,实现逻辑代码

    from django.shortcuts import render,HttpResponse
    import json
    
    def test(request):
        return render(request,'ajax.html')
    
    def ajax(request):
        if request.method=="POST":
            name=request.POST.get('name')
            print("ok")
            status=1
            result="sucuss"
            return HttpResponse(json.dumps({
                "status":status,
                "result":result,
                "name":name
            }))
  • 相关阅读:
    Flex 布局语法教程
    2017年总结的前端文章——border属性的多方位应用和实现自适应三角形
    html 里 checkbox里 只要选中就会自动添加checked=“checked”么?
    jQuery遍历DOM
    checkbox 全选操作
    ubuntu下安装jdk
    ubuntu下安装nodejs
    nodejs express route 的用法
    聊天室业务分析
    一般使用场景
  • 原文地址:https://www.cnblogs.com/weihu/p/8821972.html
Copyright © 2011-2022 走看看