解决Django的POST请求接口返回403错误


解决办法:使用@csrf_exempt装饰器,避开CsrfViewMiddleware的检测 
示例:

from django.views.decorators.csrf import csrf_exempt 
from django.http import HttpResponse

import json


@csrf_exempt 
def my_view(request): 
    resp_dict = {
        'c': -1,
        'm': 'fail',
        'd': []
    }
    if request.method == 'POST':
        resp_dict['c'] = 0
        resp_dict['m'] = 'success'
    else:
        resp_dict['c'] = -2
        resp_dict['m'] = 'request method error'
    return HttpResponse(json.dumps(resp_dict), content_type="application/json")

猜你喜欢

转载自blog.csdn.net/weixin_37773766/article/details/80413201