0
votes

my error in browser:

POST http://127.0.0.1:8000/tree/ajax/move-card/ 403 (FORBIDDEN)

Remote Address:127.0.0.1:8000
Request URL:http://127.0.0.1:8000/tree/ajax/move-card/
Request Method:POST
Status Code:403 FORBIDDEN
Request Headersview source
Accept:
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,fa;q=0.6
Cache-Control:no-cache
Connection:keep-alive
Content-Length:33
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:127.0.0.1:8000
Origin:http://127.0.0.1:8000
Pragma:no-cache
Referer:http://127.0.0.1:8000/tree/bored/
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
id_card:3
id_status:2
position:-1
Response Headersview source
Content-Type:text/html
Date:Wed, 09 Jul 2014 06:00:34 GMT
Server:WSGIServer/0.1 Python/2.7.5
X-Frame-Options:SAMEORIGIN

in js:


$.ajax({
        url: "/tree/ajax/move-card/",
        type: "POST",
        data: {
            'id_card': id_card,
            'id_status': id_status,
            'position': position
        },
        success: function (data) {
            console.log(data)
        },
        error: function () {
            alert('کار نمی کنه');
        }
    });

in view:


def move_card(request):
    response_data = {'success': 'True'}
    return HttpResponse(json.dumps(response_data ), content_type="application/json")
1

1 Answers

1
votes

Add csrf_exempt decorator to move_card function or add csrfmiddlewaretoken to ajax request data.

take a look this: https://docs.djangoproject.com/en/dev/ref/contrib/csrf/

example code:

add csrf_exempt to function

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def move_card(request):
    response_data = {'success': 'True'}
    return HttpResponse(json.dumps(response_data ), content_type="application/json")

add csrfmiddlewaretoken to post data

check this: https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax