Simple Ajax/Codeigniter request

前端 未结 8 797
無奈伤痛
無奈伤痛 2021-01-25 15:43

I\'m having some issues with ajax and codeigniter. I\'ve already posted another question (link to question) and I thought I solved it, but I did not so I`m asking someone to wri

8条回答
  •  长情又很酷
    2021-01-25 16:13

    You must submit the CSRF token from cookies otherwise the request will be invalid, if you have CSRF enabled in config.php.

    You can use this plugin to retrieve cookies in javascript. And simply pass it to CI.

    ci_token

    and

    ci_cookie

    keys may be different and can be found in config.php

    I would also suggest setting up a route for the request and using

    site_url()

    over

    base_url()

    var SITE = "" // GLOBAL variable so your javascripts can be external
    

    -

    var data = { 'ci_token' : $.cookies.get('ci_cookie'), 'increase' : parseInt(number)}
    $.ajax({
        url : SITE + "/link/to/controller/method",
        data : data,
    });
    

提交回复
热议问题