gpt4 book ai didi

javascript - django ajax 禁止发布 403

转载 作者:可可西里 更新时间:2023-11-01 01:56:15 24 4
gpt4 key购买 nike

当我尝试从我的 javascript 发帖到我的 django 服务器时,我使用 django 1.4 收到 403 错误。尽管问题仅出在帖子上,但我的工作正常。也试过@csrf_exempt 没有运气

更新:我现在可以发布我添加的 {% csrf_token %} ,但是发布响应是空的,尽管 GET 正确,有什么想法吗?

我的 django View :

@csrf_protect
def edit_city(request,username):
conditions = dict()

#if request.is_ajax():
if request.method == 'GET':
conditions = request.method

elif request.method == 'POST':
print "TIPO" , request.GET.get('type','')
#based on http://stackoverflow.com/a/3634778/977622
for filter_key, form_key in (('type', 'type'), ('city', 'city'), ('pois', 'pois'), ('poisdelete', 'poisdelete'), ('kmz', 'kmz'), ('kmzdelete', 'kmzdelete'), ('limits', 'limits'), ('limitsdelete', 'limitsdelete'), ('area_name', 'area_name'), ('action', 'action')):
value = request.GET.get(form_key, None)
if value:
conditions[filter_key] = value
print filter_key , conditions[filter_key]

#Test.objects.filter(**conditions)
city_json = json.dumps(conditions)

return HttpResponse(city_json, mimetype='application/json')

这是我的 javascript 代码:

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;
}
var csrftoken = getCookie('csrftoken');

function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
function sameOrigin(url) {
// test that a given url is a same-origin URL
// url could be relative or scheme relative or absolute
var host = document.location.host; // host + port
var protocol = document.location.protocol;
var sr_origin = '//' + host;
var origin = protocol + sr_origin;
// Allow absolute or scheme relative URLs to same origin
return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
(url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
// or any other URL that isn't scheme relative or absolute i.e relative.
!(/^(\/\/|http:|https:).*/.test(url));
}
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
// Only send the token to relative URLs i.e. locally.
xhr.setRequestHeader("X-CSRFToken",
$('input[name="csrfmiddlewaretoken"]').val());
}
}
});

$.post(url,{ type : type , city: cityStr, pois: poisStr, poisdelete: poisDeleteStr, kmz: kmzStr,kmzdelete : kmzDeleteStr,limits : limitsStr, area_nameStr : area_nameStr , limitsdelete : limitsDeleteStr},function(data,status){
alert("Data: " + data + "\nStatus: " + status);
console.log("newdata" + data.area_name)
});

我也曾在该网站上尝试过,但没有成功:

$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && sameOrigin(settings.url)) {
// Send the token to same-origin, relative URLs only.
// Send the token only if the method warrants CSRF protection
// Using the CSRFToken value acquired earlier
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});

我错过了什么?

最佳答案

您实际上可以将它与您的数据一起传递 {csrfmiddlewaretoken:'{{csrf_token}}' } ,它一直有效

关于javascript - django ajax 禁止发布 403,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13035412/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com