zoukankan      html  css  js  c++  java
  • python ajax post 数据

    简单的html

    <div>
            <input type="submit" id="tes" value="tes">
            <div id="test"></div>
    </div>

    简单的后台函数

    def tes(request):
        url = request.POST['url'] if 'url' in request.POST else 't'
        word = request.POST['word'] if 'word' in request.POST else 't'
        return HttpResponse(url+word)

    直接贴javascript

    $(document).ready(function() {
      //获取cookie函数
    function getCookie(name) { var cookieValue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; }
      //获取csrftoken对应的cookie
    var csrftoken = getCookie('csrftoken'); function csrfSafeMethod(method) { // these HTTP methods do not require CSRF protection return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); }
      #cookie写入头部 $.ajaxSetup({ beforeSend:
    function(xhr, settings) { if (!csrfSafeMethod(settings.type) && !this.crossDomain) { xhr.setRequestHeader("X-CSRFToken", csrftoken); } } }); $("#tes").click(function() { // alert("t"); // $.get('http://127.0.0.1:8000/search', function(data) { // optional stuff to do after success // $("#test").append(data); // }); var csrftoken = $.post('tes/', { url:'url', word:'word', }, function(data, textStatus, xhr) { /*optional stuff to do after success */ $("#test").append(data); }); }); });

     结果:

  • 相关阅读:
    ⑦linux pidstat
    ⑥linux mpstat
    ⑤linux 系统负载
    ④linux 进程优先级
    ③linux 进程管理
    ②linux 监控进程状态
    ①linux 进程概述于生命周期
    ④linux 自动挂载
    ③linux Gdisk
    ②linux fdisk
  • 原文地址:https://www.cnblogs.com/qwj-sysu/p/3912697.html
Copyright © 2011-2022 走看看