0

I've been searching a lot on this site and trying to find a question that might be similar to this.

I am passing a Json post request and retrieve the correct result that I expected, but I have no luck when attempting to save it into a cookie. I use js.cookie plugin for this.

the code :

$(".image-upload").on('change', '#file-input', function() {
  if (this.files && this.files[0]) {
    var FR= new FileReader();
    FR.onload = function(e) {
        var datalogin = {
                appid: "100022211"
                , appsecret: "272018"
                , user: getCookie('myid')  //this cookie still work
                , file: e.target.result
            }
            $.ajax({ type: "post"
            , url: "https://myweb/api/account/upload_picture_base64"
            , data: JSON.stringify(datalogin)
            , contentType: "application/json; charset=utf-8",
            dataType: "json", //i can't use jsonp because this is post
            beforeSend: function () {

                console.log('start'); // log
            }
            , error: function (data, status) {
                console.log('error'); // log


                alert("Unknown error");
            }
            , success: function (data) {

                var p = data.picture;               
                var t = data.picture_type;
                setCookie('mypicturetype', t, '365');
                setCookie('mypicture', p, '365');
                console.log('success. data type : ' + getCookie('mypicturetype') + 'and pict= ' + getCookie('mypicture')); //tulis dalam log
                $(".mypicture").html("<img src='data:" + t + ";base64," + p + "' height='100%' width='100%'/>");
                //success update the picture in html but fail to play cookie
            }
        });

    };       
    FR.readAsDataURL( this.files[0] );
  }
});

The result of my post request:

{"id":"2","username":"Yukon","name":"","email":"lordyukon@gmail.com","picture_type":"image\/jpeg","picture":"\/9j\/4AAQSkZJRgABAQAAAQABAAD\/\/gA8Q1JFQVRPUjogZ2Qta/\/Z","status":"0"}
mhatch
  • 4,441
  • 6
  • 36
  • 62
Pegasus Cozza
  • 129
  • 1
  • 4

1 Answers1

0

I am facing the same problem, but after few try and error, i ve figured out that you can't save base_64 string directly to a cookie.

I suggesting you to use local storage to save such type of string, instead of forcing cookie.

This Question give awesome solution for you

Community
  • 1
  • 1
Elchie
  • 1