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 回答
0

对于 Fuel 中的 ajax 调用,请使用扩展 Controller_Rest 的控制器

于 2013-08-21T18:23:37.267 回答