-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.

  • Why are you setting __content-type__ to _false_ – Munim Munna Feb 01 '18 at 21:44
  • console prints "testingtesting2" I am setting contentType to false because I hope to upload a file later. And in order to upload a file I have to set the contentType and ProcessData to false. – hello408 Feb 01 '18 at 21:45
  • you should use a `FormData` Object for this – Atural Feb 01 '18 at 22:13
  • print_r($_POST) to see if you are getting anything – Alex Feb 01 '18 at 22:19
  • 1
    try like this: `url:'/admin/upload_paper',` – Carlos Quintero Feb 02 '18 at 01:36
  • Hi Alex, print_r($_POST) just gives Array ( ). I tried using FormData and the new url but the return is the same. When I attempted to perform the upload through either post_data or FormData I receive the error "you did not select a file to upload." – hello408 Feb 02 '18 at 15:03

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