首页 > 编程语言 > 详细

python ajax post 数据

时间:2014-08-14 16:29:18      阅读:391      评论:0      收藏:0      [点我收藏+]

简单的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); }); }); });

 结果:

bubuko.com,布布扣

 

python ajax post 数据,布布扣,bubuko.com

python ajax post 数据

原文:http://www.cnblogs.com/qwj-sysu/p/3912697.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!