Ajax request not working properly in django

后端 未结 2 1365
臣服心动
臣服心动 2021-01-27 10:09

JS code for ajax request



        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-27 10:30

    It is normal, it's beacause of the CSRF token. Try this:

    var csrftoken = $('[name="csrfmiddlewaretoken"]').val();
    

    And after include this in your Ajax request

    $.ajax({
     ...
     headers: {
        'Accept': 'application/json', //if json
        'Content-Type': 'application/json', //if json
        "X-CSRFToken": csrftoken
        },
        credentials: 'include',
     ....
    })
    

    It worked for me

提交回复
热议问题