1.对get请求直接返回参数

如果请求多个参数,也只能返回一个参数,这里只返回了username参数

如果想要返回多个参数值,可以返回json格式数据

2.对get请求返回json数据

# views.py# 对get请求返回json格式数据

from django.shortcuts import renderfrom django.http.response import HttpResponsefrom django.shortcuts import render_to_responseimport json
def Login(request):
    if request.method == "GET":
        result = {}
        username = request.GET.get("username")
        mobile = request.GET.get("mobile")
        data = request.GET.get("data")
        result["user"] = username
        result["mobileNum"] = mobile
        result["data"] = data
        result = json.dumps(result)
        return HttpResponse(result, content_type="application/json", charset="utf-8")
    else:
        return render_to_response("login.html")

  

测试结果如下,如果验证不生效可以重新运行一下manage.py启动项目

3.对post请求返回json数据

# views.py

from django.shortcuts import render
from django.http.response import HttpResponse
from django.shortcuts import render_to_response
import json

def Login(request):
    if request.method == "POST":
        result = {}
        # username和password是HTML中form表单的name属性, 和HTML表单中的填写项一一对应
        username = request.POST.get("username")
        mobile = request.POST.get("password")
        result["user"] = username
        result["mobileNum"] = mobile
        result = json.dumps(result)
        return HttpResponse(result, content_type="application/json", charset="utf-8")
    else:
        return render_to_response("login.html")

  

MOOC(2)-Django开发get、post请求,返回json数据的更多相关文章

  1. 在使用Ajax请求返回json数据的时候IE浏览器弹出下载保存对话框的解决方法

    在使用Ajax请求返回json数据的时候IE浏览器弹出下载保存对话框的解决方法 最近在做一个小东西,使用kindeditor上传图片的时候,自己写了一个上传的方法,按照协议规则通过ajax返回json ...

  2. php的curl获取https加密协议请求返回json数据进行信息获取

    <?php header("Content-type:text/html; charset=utf-8");function getToken($url){        $ ...

  3. ajax请求返回json数据弹出下载框的解决方法

    将返回的Content-Type由application/json改为text/html. 在struts2下: <action name="XXXAjax" class=& ...

  4. AJAX请求返回JSON数据动态生成html

    1:DeliveryPersonVO对象 package com.funcanteen.business.entity.delivery.vo; import java.util.List; impo ...

  5. swift http请求返回json数据和分析

    1 AppDelegate.swift // // AppDelegate.swift // QQDemo // // Created by 赵超 on 14-6-21. // Copyright ( ...

  6. 前台返回json数据的常用方式+常用的AJAX请求后台数据方式

    我个人开发常用的如下所示: 之所以像下面这样下,一是前台Ajax,二是为安卓提供接口数据 现在常用的是返回JSON数据,XML的时代一去不复返 JSON相对于XML要轻量级的多 对JSON不是十分熟悉 ...

  7. 如何在Crystal Portlet中正确返回JSON数据给AJAX请求?

    当Crystal Portlet中需要采用Ajax请求,并让后台返回Json数据时,如何才能正确.方便的返回Json数据呢? 以下两种方法均可: 方法一:Ajax请求时,采用RenderURL,对应P ...

  8. J2EE Web开发入门—通过action是以传统方式返回JSON数据

    关键字:maven.m2eclipse.JSON.Struts2.Log4j2.tomcat.jdk7.Config Browser Plugin Created by Bob 20131031 l ...

  9. Query通过Ajax向PHP服务端发送请求并返回JSON数据

    Query通过Ajax向PHP服务端发送请求并返回JSON数据 服务端PHP读取MYSQL数据,并转换成JSON数据,传递给前端Javascript,并操作JSON数据.本文将通过实例演示了jQuer ...

随机推荐

  1. 常用DIV+CSS命名大全集合

    一.命名规则说明:   -   TOP 1).所有的命名最好都小写 2).属性的值一定要用双引号("")括起来,且一定要有值如class="divcss5",i ...

  2. 细说opcache

    ; opcache的开关,关闭时代码不再优化. opcache.enable=1 ; Determines if Zend OPCache is enabled for the CLI version ...

  3. IaaS SaaS PaaS区别

  4. 吴裕雄--天生自然 JAVA开发学习:流(Stream)、文件(File)和IO

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //使用 BufferedReader 在控制台读取 ...

  5. Python—插入排序算法

    # 插入排序,时间复杂度O(n²) def insert_sort(arr): """ 插入排序:以朴克牌为例,从小到大排序.摸到的牌current与手里的每张牌进行对比 ...

  6. Python笔记_第二篇_面向过程_第二部分_1.函数

    函数:这个词属于一个数学概念,在编程语言借鉴了这个概念,表现形式是一段程序代码的组合,也叫“程序集”.有过编程基础的人很容易理解这个概念,当我们编写程序越来越多的时候,程序设计大师们会把散乱的程序进行 ...

  7. HTTP Error 502.5 - Process Failure 解决方案

    .netcore 2.1.4的程序部署到IIS后报以下错误: ======================================================= HTTP Error 50 ...

  8. 如何动态调用WebService

    封装WBS类 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sy ...

  9. 通过Dockerfile 文件为linux images 添加新用户

    要求: (1)增加一个新用户,名为mynewuser (2)让这个用户有root权限 (3)设置其密码为mynewpassword (4)Container启动后以mynewuser登录,并且直接到m ...

  10. tensorflow训练Oxford-IIIT Pets

    参考链接https://github.com/tensorflow/models/blob/master/object_detection/g3doc/running_pets.md 先参考https ...