ZendFramework Ajax jQuery帖子

时间:2012-09-03 18:16:13

标签: jquery ajax zend-framework http-post

你好我在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 Order Controller中的代码:

    $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>

1 个答案:

答案 0 :(得分:1)

当您点击提交按钮时,会在提交表单的同时触发ajax调用。

如果没有action="",您可能想知道为什么要提交表单。

问题是,当action属性为空时,它假定它在同一页面上。我的意思是,如果您使用localhost/index.php而不是action="" = action="localhost/index.php"

因此,您必须通过添加action="javascript:void(0)"来停用表单提交。

您也可以使用jQuery:

cancel a submit in jquery?

甚至普通的javascript(对提交事件返回false)