Codeigniter CSRF valid for only one time ajax request

前端 未结 9 869
鱼传尺愫
鱼传尺愫 2020-12-03 11:46

I want to upload image on the server on change event of jQuery but using codeigniter csrf I am able to upload image only one time. How can I upload images using ajax for mul

9条回答
  •  北海茫月
    2020-12-03 12:03

    add this at a js file which is loaded every page (I put this at the end of jquery.js )

        $.ajaxSetup({
            beforeSend:function(jqXHR, Obj){
                var value = "; " + document.cookie;
                var parts = value.split("; csrf_cookie_name=");
                if(parts.length == 2)   
                Obj.data += '&csrf_token='+parts.pop().split(";").shift();
            }
        });
    

    (notice that in every ajax request you can not have empty data to send)

    "csrf_cookie_name" at top defined in config.php

    $config['csrf_cookie_name'] = 'csrf_cookie_name';
    

提交回复
热议问题