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);
                }
            })
        });
    

      

  • 相关阅读:
    noip宝藏
    [HNOI2008]玩具装箱TOY
    hdu 4372 Count the Buildings 轮换斯特林数
    bzoj 3000 Big Number 估算n!在k进制下的位数 斯特林公式
    自然数幂求和方法1:扰动法(求两次)
    斯特林数 学习笔记
    hdu 3625 Examining the Rooms 轮换斯特林数
    cf 472G Design Tutorial: Increase the Constraints 分块+压位/FFT
    chef and churu 分块 好题
    分块 学习笔记
  • 原文地址:https://www.cnblogs.com/Yellow0-0River/p/5499885.html
Copyright © 2011-2022 走看看