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. 本地搭建3节点kubernetes

    kubernetes本地搭建版本选择 CentOS Linux release 7.7.1908 kubernetesVersion: v1.17.0 weave-kube:2.6.0 ceph/ce ...

  2. JAVA初学者——标识符命名规则及数据类型的转换

    Hello!我是浩宇大熊猫~ 直接进入正题吧~ 1)标识符的命名规则. 标识符命名法有小驼峰命名法和大驼峰命名法两种,分别应用于方法.变量和类. 小驼峰命名法应用于方法和变量,主要有两个约定: 1.标 ...

  3. Django知识点集合

  4. R语言 批量下载财务报表

    getsheets <- function(symbol,type,file){ pre="http://money.finance.sina.com.cn/corp/go.php/v ...

  5. 吴裕雄--天生自然TensorFlow高层封装:Estimator-DNNClassifier

    # 1. 模型定义. import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist impor ...

  6. Unity3D事件函数的执行顺序

    In Unity scripting, there are a number of event functions that get executed in a predetermined order ...

  7. Django知识点_梳理

  8. Long型转ZonedDateTime型

    /** * 将Long类型转化成0 * @author yk * @param time * @return */public static ZonedDateTime toZonedDateTime ...

  9. org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)报错

    0 环境 系统环境:win10 1 正文 先检查Mapper接口与相关联xml文件是否对应,需要检查包名,namespace位置是否写对,curd时id名称等能否对应上 常规步骤: :检查mapper ...

  10. C# 类的解构

    C#对类的解构,必须在该类内实现Deconstruct方法,并且返回类型为void ,并用out参数返回各个部分. using System; using System.Text; namespace ...