使用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

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.

Turn on error logging in PHP and see the error logs. The error logs would most likely point to the problem. If not, log the values of $sessionVal and $id from your php function; that may tell you where the problem lies.