-2

I am trying to echo back code I received from Ajax. But it does not work when I am using contentType:false, processData:false.

Here is my ajax. The url is correct. If I comment out the line with post_data['file'] and contentType:false, processData:false I will be able to get the echo, but as soon as contentType:false, processData:false is in I cannot get anything.

    post_data = {};
    post_data['file']= document.getElementById('fileToUpload').files[0];
    post_data['paper-type']=$("#paper-input :selected").val();

    $.ajax({
        url:'/admin/upload_paper',
        data: post_data,
        type: 'post',
        contentType: false,
        processData: false,
        success: function(data){
            console.log(data);
        },
        error: function(data){
            console.log(data+"error");
        }
    });

Here is the code snippet from CI

public function upload_paper(){       

    echo $this->input->post('paper-type');
    echo "testing";
    echo "testing2";

}

Does anyone know what that is? Thank you.

1 Answers1

0

instead of doing this:

post_data['paper-type']=$("#paper-input :selected").val();

try

var val = $("#paper-input :selected").val();

and in ajax:

$.ajax({
    url:'/admin/upload_paper',
    data: {'paper-type':val},
    type: 'post',
    success: function(data){
        console.log(data);
    },
    error: function(data){
        console.log(data+"error");
    }
});
kamote ulalo
  • 162
  • 5
  • Hi Kamote, thank you for your help. It does work, but it stops working when I add in the contentType:false and processData:false. And I need those in order to upload a file later. – hello408 Feb 02 '18 at 14:59
  • try looking here https://stackoverflow.com/questions/20795449/jquery-ajax-form-submission-enctype-multipart-form-data-why-does-contentt – kamote ulalo Feb 03 '18 at 10:08