2016-10-06 8 views
0

私はこのサイトで多くを探しており、これに似た質問を探しています。JQUERY-AJAX-JSON、データの結果をクッキーに保存できません

私はJsonの投稿要求を渡して、私が期待した正しい結果を取得していますが、それをCookieに保存しようとすると運がありません。私はこれのためにjs.cookieプラグインを使います。

コード:

$(".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]); 
    } 
}); 

私のポスト要求の結果:

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

答えて

0

私は同じ問題に直面していますが、いくつかの試行錯誤の後、私は「あなたができることを考え出しましt base_64文字列をクッキーに直接保存します。

ローカルストレージを使用して、クッキーを強制する代わりに、このような文字列を保存することをお勧めします。

This Question give awesome solution for you

関連する問題