python ajax post 数据
2024-08-24 05:18:47
简单的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);
});
});
});
结果:
最新文章
- 使用UG UISTYLER 窗体编辑器,创建对话框 part 2
- stl 初步的使用
- c++ DISALLOW_COPY_AND_ASSIGN
- Ant build ${renderscript.opt.level}问题解决方案
- 02-自定义CALayer
- MySQL数据库双机热备份
- MySQL的alter的使用
- Hrbust1328 相等的最小公倍数 (筛素数,素因子分解)
- VS2010旗舰版安装图解
- NFS(网络文件系统)
- PHP PSR-3 日志接口规范 (中文版)
- Oracle EBS-SQL (SYS-24):职责列表
- MyBatis 笔记总结
- MySQL服务相关
- Python 黑客相关电子资源和书籍推荐
- Spring Security 入门(3-10)Spring Security 的四种使用方式
- 学JAVA第六天,运算符、表达式、if语句以及for、while、都循环
- js 校验 btc eth 地址
- 作业七:Linux内核如何装载和启动一个可执行程序
- Java学习笔记之Linux下的Java安装和配置
热门文章
- 关于百度Editor富文本编辑器 自定义上传位置
- python中字典的循环遍历的两种方式
- 【iOS开发】iOS CGRectGetMaxX/Y 使用
- Android问题:could not install *smartsocket* listener;Address already in use
- PHP变量的实现原理【转】
- 【题解】NOI2009管道取珠
- [洛谷P2147][SDOI2008]洞穴勘测
- NOIP2016愤怒的小鸟 [状压dp]
- HDU 4417 划分树写法
- centos关闭ipv6