django ajax报错解决:You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set.
Django版本号:1.11.15
django中ajax请求报错:
You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data.
将from的action地址改为/结尾的就可以了
或者
修改settings:APPEND_SLASH=False
修改之后提示
Forbidden (403)
CSRF verification failed. Request aborted.
Help
Reason given for failure:
CSRF cookie not set.
方法1:不使用CSRF验证
全站禁用(不推荐)
去掉settings.py中MIDDLEWARE中的django.middleware.csrf.CsrfViewMiddleware中间件
例如如下配置,去掉django.middleware.csrf.CsrfViewMiddleware即可
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
局部禁用(推荐)
或者在不想进行csrf保护的view可以加上@csrf_exempt
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def ajaxGetList(r):
方法2:使用CSRF验证
django1.11版csrf官方文档:https://docs.djangoproject.com/en/1.11/ref/csrf/#django.views.decorators.csrf.csrf_protect
form表单中添加
{% csrf_token %}
views.py代码
from django.template.context_processors import csrf
from django.http import HttpResponse
from django.template import Context, loader
def my_view(request):
c = {}
c.update(csrf(request))
# ... view code here
return HttpResponse(loader.get_template('index.html').render(c))
旧版本的代码:
from django.core.context_processors import csrf
from django.shortcuts import render_to_response
def my_view(request):
c = {}
c.update(csrf(request))
# ... view code here
return render_to_response("a_template.html", c)
js代码
在发 ajax POST 请求时,加一个 X_CSRFTOKEN 的 header
// using jQuery
var csrftoken = jQuery("[name=csrfmiddlewaretoken]").val();
或者
var csrftoken = $.cookie('csrftoken');
代码1:
function submitForm(){
var user = $('#user').val();
$.ajax({
url: '/csrf1.html',
type: 'POST',
headers:{'X-CSRFToken': csrftoken},
data: { "user":user},
success:function(arg){
console.log(arg);
}
})
}
代码2:
// 去cookie中获取值
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
function DoAjax(){
$.ajax({
url: '/csrf/',
type: 'POST',
data: {'k1': 'v1'},
success: function (data) {
console.log(data);
}
})
}
PS:
1.csrf 装饰器
全局:
中间件 django.middleware.csrf.CsrfViewMiddleware
局部:
from django.views.decorators.csrf import csrf_exempt,csrf_protect
@csrf_protect,为当前函数强制设置防跨站请求伪造功能,即便settings中没有设置全局中间件。
@csrf_exempt,取消当前函数防跨站请求伪造功能,即便settings中设置了全局中间件。
2.django建议使用django.middleware.csrf.CsrfViewMiddleware进行全局控制,不提倡使用@csrf_protect进行单view控制,因为这样可能会有遗漏。如果不想进行csrf保护的view可以加上@csrf_exempt。 使用CSRF验证:把django.core.context_processors.csrf加到配置文件的 TEMPLATE_CONTEXT_PROCESSORS,或者手动生成csrftoken并加到template context。
django ajax报错解决:You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set.的更多相关文章
- Python Django migrate 报错解决办法
1. 在现有基础上又添加一个表的时候migrate报错 migrate报错django.db.utils.OperationalError: (1050, "Table 'cmdb_eidc ...
- django.db.utils.InternalError: (1060, "Duplicate column name 'user_id'")迁移报错解决方法
django.db.utils.InternalError: (1060, "Duplicate column name 'user_id'")迁移报错解决方法 django.db ...
- Django独有报错的原因和解决
RuntimeError at /login You called this URL via POST, but the URL doesn't end in a slash and you have ...
- python---补充django中文报错(1),Django2.7使用sys.setdefaultencoding('utf-8'),以及使用reload(sys)原因
SyntaxError at /blog/ news/story Non-ASCII character , but no encoding declared; see http://python.o ...
- Django 启动报错 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7
pycharm 报错 cmd 报错 解决办法 首先 是计算机 编码问题 是 django 读取你的 用户host名 但是 windos 用户名 如果是中文 就会报这个错 要改成 英文
- sphinx :undefined reference to `libiconv' 报错解决办法
sphinx :undefined reference to `libiconv' 报错解决办法 2013-11-30 21:45:39 安装sphinx时不停报错...郁闷在make时报错,错误 ...
- redis运用连接池报错解决
redis使用连接池报错解决redis使用十几小时就一直报异常 redis.clients.jedis.exceptions.JedisConnectionException: Could not g ...
- linux下启动dbca或netmgr类的图形界面报错解决
linux下启动dbca或netmgr类的图形界面报错解决 Xlib: connection to ":0.0" refused by server Xlib: No pro ...
- CentOS 6.5 Maven 编译 Apache Tez 0.8.3 踩坑/报错解决记录
最近准备学习使用Tez,因此从官网下载了最新的Tez 0.8.3源码,按照安装教程编译使用.平时使用的集群环境是离线的,本打算这一次也进行离线编译,无奈一编译就开始报缺少jar包的错,即使手动下载ja ...
随机推荐
- 手机号的 AES/CBC/PKCS7Padding 加解密
前言:接口中上次的手机号码和密码是传入的加密的,模拟自动化的时候也需要先对数据进行加密 1.各种语言实现 网上已经各种语言实现好的AES加密,可以点击查看:http://outofmemory.cn/ ...
- 六、在U-boot中让LCD填充纯色
1. 编译U-boot 准备好U-boot压缩包urbetter-u-boot-1.1.6-v1.0.tgz,输入命令:tar -xvf urbetter-u-boot-1.1.6-v1.0.tgz ...
- ie 折腾计(浏览器兼容性)
常见问题 IE:6.0,IE7.0,IE8.0之间的兼容独立说明 /*用于展示标签*/ <div class="jrx"></div> <style ...
- 看我怎么扒掉CSDN首页的底裤(python selenium+phantomjs爬取CSDN首页内容)
这里只是学习一下动态加载页面内容的抓取,并不适用于所有的页面. 使用到的工具就是python selenium和phantomjs,另外调试的时候还用了firefox的geckodriver.exe. ...
- Eclipse_设置_01_自动提示
- kafka definitive guide - reading notes
一.认识Kafka 1)什么是sub/pub模型, 发布订阅模型 Publish/subscribe messaging is a pattern that is characterized by ...
- 九度OJ1020-最小正方形-判大小
题目1020:最小长方形 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7410 解决:3521 题目描述: 给定一系列2维平面点的坐标(x, y),其中x和y均为整数,要求用一个 ...
- xdoj-1298(模拟--简易SQL解释器)
题目链接 一 知识点: 1 substr有2种用法: 假设:string s = "0123456789"; string sub1 = s.substr( ...
- PAT-1084(外观数列 ) && PAT-1085 (PAT单位排行)
1084 利用字符串string的可加性 #include <bits/stdc++.h> using namespace std; int main () { int x,n; cin ...
- Formal Grammars of English -10 chapter(Speech and Language Processing)
determiner 限定词 DET propernoun 专有名词 NP (or noun phrase) mass noun 不可数名词 Det Nouns 限定词名词 relative pro ...