0

このお気に入りの配列をphpに送信する最良の方法を知りたいのですが、ajaxを使用しようとしていますが、403禁止エラーが発生し続けます。パスは正しいです。ここで何か間違ったことをしているに違いありません。助けていただければ幸いです。

$(function(){
    var favorite = localStorage.getItem( 'favorite' );
    if (favorite  !== null){
        favorite = JSON.parse(favorite) || [];
    }
    $('.favorites' ).each(function() {
        var petid = $(this).attr('data-petid');
        if(favorite.indexOf(petid) !== -1){
            $(this).css('background-image', 'url(../assets/img/heart-red.svg)');
            $(this).css('background-color', '#fefefe');
        }
    });
     // This function changes the color of the heart on the landing page and stores the values into local storage
    $(".favorites").click(function() {

       var favorite = localStorage.getItem( 'favorite' );
       var petid = $(this).attr('data-petid');
       var index;

       favorite = JSON.parse(favorite) || [];

       if ((index = favorite.indexOf(petid)) === -1) {
          favorite.push(petid);
          $(this).css('background-image', 'url(../assets/img/heart-red.svg)');
          $(this).css('background-color', '#fefefe');
       }else {
          $(this).css('background-image', 'url(../assets/img/heart-full.svg)');
          $(this).css('background-color', '#25aae3');
          favorite.splice(index, 1);
       }
       localStorage.setItem('favorite', JSON.stringify(favorite) );
       $.ajax({
          type: "POST",
          url: '/petlist/fuel/app/views/site/favorites.php',
          data: favorite,
          success: function(response){
            console.log(response);
          }

      });
    });

 });
4

2 に答える 2

0

ajax リクエストは次のようになります。

$.post('/petlist/fuel/app/views/site/favorites.php', { "favorite" : favorite }, function(data) { 
    console.log(data); 
});

次に、PHPで、次の方法でデータにアクセスできます

print_r($_POST['favorite']);
于 2013-08-21T16:55:33.900 に答える