zoukankan      html  css  js  c++  java
  • jQuery的ajax与django传参

    Get方式传参

    Django中的代码如下:

    • urls.py代码:
    from django.conf.urls import url
    from django.contrib import admin
    import AjaxTest.views
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r"^index/$",AjaxTest.views.index),
    ]
    • views.py代码:
    from django.http import HttpResponse
    
    def index(req):
        print req.GET.get('url')
        if req.GET.get('url')=='test':
            return HttpResponse("hello,this is a test")
        else:
            return HttpResponse("hahahaha")

    jQuery中的代码如下:

    • 方式1:
    $("input").click(function() {
    	$.get("/index/?url=test", function (response, status, xhr) {
    		$(".box").html(response);
    	});
    });
    • 方式2:
    $("input").click(function() {
        $.get("/index/", "url=test", function (response, status, xhr) {
            $(".box").html(response);
        });
    });
    • 方式3:
    $("input").click(function() {
    	$.get("/index/",{
    		url:"test"
    	},function(response,status,xhr){
    		$(".box").html(response);
    	});
    });
    

    POST方式传参

    Django中的代码如下:

    • urls.py代码:
    from django.conf.urls import url
    from django.contrib import admin
    import AjaxTest.views
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r"^index/$",AjaxTest.views.index),
    ]
    •  views.py代码如下:
    def index(req):
        if req.POST.get('url')=='test':
            return HttpResponse("htllo,this is a test")
        else:
            return HttpResponse("hahahahah")
    

    jQuery中的代码如下:

    • 方式1:
    $("input").click(function () {
    	$.post("/index/","url=test",function(response,status,xhr){
    		$(".box").html(response)
    	});
    });
    
    • 方式2:
    $("input").click(function(){
    	$.post("/index/",{
    		url:"test"
    	},function (response,status,xhr) {
    		alert(status);
    		$(".box").html(response)
    	});
    });
    
    • 方式3:
       $("input").click(function(){
            $.ajax({
                type:"POST",
                url:"/index/",
                data:{url:"test"},
                success:function (response,status,xhr) {
                    $(".box").html(response);
                }
            })
        });
    

      

  • 相关阅读:
    剑指Offer——数组中重复的数字
    基于Google Protobuff和Mina的RPC
    Google Protocol Buffers 编码(Encoding)
    Protocol Buffers 语法指南
    Google Protocol Buffers 入门
    Google Protocol Buffers 概述 转
    Protobuf语言指南
    基于Protobuf的通讯库--Poppy简介
    log4cxx第三篇----使用多个logger
    log4CXX第二篇---配置文件(properties文件)详解
  • 原文地址:https://www.cnblogs.com/Yellow0-0River/p/5499885.html
Copyright © 2011-2022 走看看