duanguai2781 2015-07-07 10:57
浏览 29
已采纳

使用ajax从会话中删除项目[关闭]

I have a problem with my code, So I tried to delete an element from session using ajax request. My link in html :

<a style="padding-left:5px;" href="#" onclick="removeItemFromSession({{ product['product_id'] }})" title="Remove this item">Remove</a>

My ajax removeItemFromSession() methode :

 <script type="application/javascript">
    function removeItemFromSession(id){
        console.log(id);
        var id = id,
        url_deploy = "http://"+window.location.hostname+":1234"+"/cartItems/delete";
        console.log(url_deploy);
        $.ajax({
            url: url_deploy,
            type: "POST",
            async: true,
            data: { id:id},
            success: function(data){
                document.location.reload(true);
            },
            error: function(){
            }
        });
    }
</script>

The route for /cartItems/delete :

shoppingCart_delete:
path: /cartItems/delete
defaults: { _controller: ShopDesktopBundle:Basket:delete }
requirements:
    _method:  GET|POST

My delete method in controller:

 public function deleteAction(){
    $id = $_POST['id'];
    print_r($id);
    $sessionVal = $this->get('session')->get('aBasket');
    unset($sessionVal[$id]);
}

I get the error : "NetworkError: 500 Internal Server Error - http://shop.com:1234/cartItems/delete". Can you help me please ? Thx in advance

  • 写回答

2条回答 默认 最新

  • doujiu1447 2015-07-07 11:11
    关注

    First thing: check the app/logs/prod.log for any meaningful error message. If none found check the server log (be it Apache or any other). If not production, you might want to run in _dev mode to get more verbose error message.

    Another thing: It's recommended to avoid using superglobals ($_POST,$_SESSION, $_GET,...) from within `Symfony2. The framework itself provides ways to get all things you would need.

    For example, your code above should look like this:

    public function deleteAction(){
        # Since you tagged the question with Symfony-2.1
        $id = $this->getRequest()->request->get('id');
    
        if ( $id ){
            $sessionVal = $this->get('session')->get('aBasket');
            if ( array_key_exists($id, $sessionVal)){
                unset($sessionVal[$id]);
                $sessionVal = $this->get('session')->set('aBasket', $sessionVal);
            }
        }
    }
    

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度