1

I have problem with csrf in codeigniter framework when i want to send file ajax type must :

   var file = document.getElementById("sortpicture");
   var formData = new FormData();
   formData.append("upload", file.files[0]);
   
   var csrf_name = $("input[name=csrf_name]").val();
   var csrf_token = $("input[name=csrf_token]").val();
   console.log(formData);
   var dataObj = {};
   dataObj[csrf_name]=csrf_token;
   //dataObj['file']=formData;

    $.ajax({
                url: "/uploads",
                dataType: 'script',
                cache: false,
                contentType: false,
                processData: false,
                data: form_data,                         
                type: 'post',
                success: function(){
                    alert("works"); 
                }
     });

But When i use data like

data {<?php echo $this->security->get_csrf_token_name();?> : <?php echo $this->security->get_csrf_hash();?>},

It must ignore contentType: false, processData: false, to reslove then i can't send file now :(

I am tried to search how send file with ajax in codeigniter framework

thank you everyone :)

Keyvan
  • 51
  • 1
  • 8
  • Whats an error are you getting within it define it – Narendrasingh Sisodia Mar 02 '15 at 12:57
  • The action you have requested is not allowed – Keyvan Mar 02 '15 at 12:58
  • Have you checked your answer somewhere else just check this link if it works for you [Link1](http://stackoverflow.com/questions/5367096/csrf-token-problem-with-codeigniter?rq=1), [Link 2](http://stackoverflow.com/questions/21214612/codeigniter-csrf-error-the-action-you-have-requested-is-not-allowed) revert me back if you found your answer over there – Narendrasingh Sisodia Mar 02 '15 at 13:06
  • Yes , i saw it :) i can send any text message to ci when don't use contentType , processData in ajax(jqery method ) but in fromData when disable this method i got error from jquery :( – Keyvan Mar 02 '15 at 13:10
  • You could do this manually as well by adding the token as a hidden input field in the form `` let's see if it works – Narendrasingh Sisodia Mar 02 '15 at 13:14
  • I have these let me update code then see it – Keyvan Mar 02 '15 at 13:16
  • problem is not crf token name or hash problem is file data not able to send – Keyvan Mar 02 '15 at 13:17
  • You have console.log(form_data) are you getting form_data, if Yes then in which format – Narendrasingh Sisodia Mar 02 '15 at 13:22
  • i have form data but i can't send form data and csrf both if i send these together i must commet content type and porecces data then form data not valid to be send ( i get error ) (javascript error I mean) – Keyvan Mar 02 '15 at 17:08
  • Then try to send form data individually by passing reference parameter – Narendrasingh Sisodia Mar 03 '15 at 05:27
  • form_data is object when you want send it none varibale can't send it in array :( – Keyvan Mar 13 '15 at 17:15
  • Possible duplicate of [Action you have requested is not allowed error](http://stackoverflow.com/questions/10383483/action-you-have-requested-is-not-allowed-error) – Paul Sweatte Nov 22 '15 at 04:09

0 Answers0