【发布时间】:2015-09-07 13:29:38
【问题描述】:
我有一个问题,所以我尝试使用 ajax 从会话中删除一个元素。 我的路线是:
shoppingCart_delete:
path: /cartItems/delete/{id}
defaults: { _controller: ShopDesktopBundle:Basket:delete }
requirements:
id: \d+
_method: POST
我的功能删除:
public function deleteAction($id){
$sessionVal = $this->get( 'session' )->get( 'aBasket' );
unset($sessionVal[$id]);
}
我的 ajax 代码:
<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>
还有我的html:
<a href="#" onclick="removeItemFromSession({{ product['product_id'] }})" title="Remove this item">Remove</a>
我在控制台中看到错误:
"NetworkError: 404 Not Found - http://shop.com:1234/cartItems/delete"
你能帮帮我吗?提前谢谢!!
【问题讨论】:
-
当你直接尝试访问shop.com:1234/cartItems/delete URL时会发生什么?
-
我得到一个 404 :找不到“GET /cartItems/delete”的路线
-
这是预期的,因为您已经定义了到“/cartItems/delete/{id}”的路径,而不仅仅是“/cartItems/delete”。尝试访问“/cartItems/delete/1”,我确信您的 deleteAction 函数将被执行。这里的“1”可以是任何id。
-
您在 ajax 中指定了 POST 方法。尝试使用 GET 方法,以便您的 url 将与定义的路由文件匹配。并且参数 id 将作为 url 参数传递
标签: javascript php jquery ajax symfony