ajax https 403,javascript - django ajax post 403 forbidden - Stack Overflow 青旅半醒 2022-09-03 06:22 132阅读 0赞 using django 1.4 im getting a 403 error when i try to do a post from my javascript do my django server. my get works fine though the problem is only with the post. also tried @csrf\_exempt with no luck UPDATE: I can Post now that i added \{% csrf\_token %\} , but the post response comes empty, although the GET comes correctly, any ideas? my 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') here is my javascript code : 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) \}); i have also tried from the site with no luck : $.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); \} \} \}); what am i missing?
还没有评论,来说两句吧...