2016-10-06 2 views
0

Ich habe viel auf dieser Site gesucht und versucht, eine ähnliche Frage zu finden.JQUERY-AJAX-JSON, Datenergebnis konnte nicht in Cookie gespeichert werden

Ich übergebe eine JSON Post Anfrage und das richtige Ergebnis, das ich erwartet hatte, aber ich habe kein Glück, wenn Sie versuchen, es in einem Cookie zu speichern. Ich benutze js.cookie Plugin dafür.

der 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]); 
    } 
}); 

Das Ergebnis meiner Post Anfrage:

{"id":"2","username":"Yukon","name":"","email":"[email protected]","picture_type":"image\/jpeg","picture":"\/9j\/4AAQSkZJRgABAQAAAQABAAD\/\/gA8Q1JFQVRPUjogZ2Qta/\/Z","status":"0"} 

Antwort

0

Ich bin vor dem gleichen Problem, aber nach wenigen Versuch und Irrtum, ich habe herausgefunden, dass man‘ t Speichere die base_64-Zeichenfolge direkt in einem Cookie.

Ich empfehle Ihnen, lokalen Speicher zu verwenden, um diese Art von Zeichenfolge zu speichern, anstatt Cookie zu erzwingen.

This Question give awesome solution for you

Verwandte Themen