你好,我有一个关于ZendFramework的jQuery ajax post请求的问题,这是我的jquery:
$("input[type=image].addToCartButton").click(function() {
var productId = $(this).attr("id");
$.ajax({
type: 'POST',
dataType: 'html',
url: "http://localhost/ZendProjects/essence/essenceZP/public/order",
data: {
id: productId
},
success: function(data){
alert(data);
}
});
});下面是我在ZendFramework订单控制器中的代码:
$request = $this->getRequest();
if($request->isXmlHttpRequest()) {
$this->_helper->layout()->disableLayout();
$test = 'testing ajax content';
$this->view->test = $test;
} else {
$test = 'testing non-ajax content';
$this->view->test = $test;
}在firebug控制台中,我得到一个错误,但看不到错误在哪里,因为它显示了0.5秒并隐藏..有什么想法吗?我们的想法是单击按钮将productId发送到顺序控制器,并将其放入会话数组中,但现在仅测试ajax请求。
标记:
<form action="" method="post">
<label for=".productQuantity" style="float: left;margin-top:7px;margin-left: 5px">Количество</label>
<input class="productQuantity" name="productQuantity" type="number" value="1" min="1" max="1000" style="width: 50px;float: left;margin-left: 7px" />
<input type="image" id="<?php echo $product->id; ?>" name="<?php echo $this->url(array('controller' => 'order', 'action' => 'add-to-cart'), 'default', true) ?>" src="<?php echo $this->baseUrl('images/cartPut.png'); ?>" style="margin-left: 13px;" />
</form>发布于 2012-09-04 03:13:52
发生的情况是,当您单击submit按钮时,ajax调用将在表单提交的同时触发。
您可能想知道,如果没有action="",为什么要提交表单。
问题是,当action属性为空时,它会假定它在同一个页面上。我的意思是,如果你在localhost/index.php上而不是action="" = action="localhost/index.php"
因此,您必须通过添加action="javascript:void(0)"来禁用表单提交。
您还可以使用jQuery来执行以下操作:
或者甚至是普通的javascript (将false返回给提交事件)
https://stackoverflow.com/questions/12252466
复制相似问题