首页 > 代码库 > python ajax post 数据
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 = http://www.mamicode.com/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); }); });});
结果:
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。