0

I'm using the cordova file-transfer plugin in order for users to upload pictures within my app.

My code works perfectly when making a normal http request to the php page on my server. I would like to make a secure request so am trying to use https however I am getting a 406 error (see screenshot for error details)

enter image description here

All other ajax requests I am making within the app are working successfully using https.

I am currently not sending any headers when making the request however there is an option to do this using the file-transfer plugin.

I have looked into how I can solve this error (for example this question here) however am still uncertain as to what I need to do in my case. I was wondering can you help determine what headers I need?

Here is my code:

Javascript

function uploadProfilePic(){
    var token = localStorage.getItem("usertoken");      
    var defs = [];
    var def = $.Deferred();
    function win(r) {   

        if($.trim(r.response) === "0") {
            alert("Sorry! We have encountered an error");
            def.resolve(0);
        }else{

            def.resolve(1);     
        }
    }       
    function fail(error) {

        //upload of pic failed.
        alert("Sorry! We have encountered an error: " + JSON.stringify(error));
        def.resolve(0);
    }
    var uri = encodeURI("https://www.example.com/update_profile_pic.php");

    var options = new FileUploadOptions();
    options.fileKey="profile_pic_image_file";
    options.mimeType="image/jpeg";

    var params = new Object();
    params.usertoken = token; 
    params.app_root_url = app_root_url;

    //not sure what headers to add here.
    //var headers={'headerParam':'headerValue'};
    //options.headers = headers;

    options.params = params;


    var ft = new FileTransfer();
    ft.onprogress = function(progressEvent){
        if(progressEvent.lengthComputable){
                loadingStatus.setPercentage(progressEvent.loaded / progressEvent.total);
            }else{
                loadingStatus.increment();
            }
    };

    ft.upload($ESAPI.encoder().encodeForURL(profileImage), uri, win, fail, options);
    defs.push(def.promise());

    $.when.apply($, defs).then(function() { 
        //pic uploaded fine

    }); 
}

PHP (upload_profile_pic.php)

header("Access-Control-Allow-Origin: *");   

if(isset($_FILES['profile_pic_image_file'])){
    $data['profile_image_is_set'] = true;


    //do stuff with profile image here

    echo json_encode($data);
}else{
    $data['profile_image_is_set'] = false;
        //image not set 
    echo json_encode($data);
}
Community
  • 1
  • 1
Sarah
  • 1,943
  • 2
  • 24
  • 39
  • If the called server is not working with `https` you have to use `http`. – JustOnUnderMillions Mar 02 '17 at 14:48
  • @JustOnUnderMillions thanks. it is working with https on all other ajax request within the app. – Sarah Mar 02 '17 at 14:53
  • But a http status code of 406 is from the server itself, not from the script behind it. So there is some misconfiguration on the server. – JustOnUnderMillions Mar 02 '17 at 15:07
  • @JustOnUnderMillions so could it be to do with the server not accepting image files over https? ... (all my other https ajax requests to the server are only sending stringified JSON data.) – Sarah Mar 02 '17 at 15:12
  • I can not clearly say no or yes. sry. But `not accepting image files` is illerevant because you are calling a php-file `update_profile_pic.php` so the server does not now what realy comes in, but the server blocks it with status code 406. Sry, is not really possible to help. – JustOnUnderMillions Mar 02 '17 at 16:07
  • Found this http://stackoverflow.com/questions/26198736/http-status-406-spring-mvc-3-2-jquery-json#26198774 and look for `related` on the right side of the page for more SO Q with the same issue. – JustOnUnderMillions Mar 02 '17 at 16:08
  • @JustOnUnderMillions Thank you. Is there anyway I can debug it more (ie get more information about the error) and see if it is rejecting the security certificate or something? Do you suggest I contact my hosting to see if the SSL cert is configured correctly?thanks for the help – Sarah Mar 02 '17 at 16:22

0 Answers0