加载成功后奇怪的重定向

时间:2013-09-01 21:53:21

标签: jquery symfony symfony-2.3

我在内容加载成功后得到一个奇怪的重定向,我找不到导致这种行为的原因所以我要求一些帮助或提示解决这个问题。看到这是我正在使用的jQuery代码:

$(".sell-product").click(function() {
    var request = $.ajax({
        type: 'GET',
        url: Routing.generate('stock_exists', {company_id: company_id, product_upc: $(this).attr("data-id")}),
        success: function(data) {
            $("#layout-center").html();
            if (data.response === false) {
                $.prompt(data.msg, {
                    title: "Este producto ya posee stock, quiere actualizarlo?",
                    buttons: {"Sí": true, "No": false},
                    submit: function(e, v, m, f) {
                        if (v === true) {
                            // redireccionamos a otro lado
                        }
                    }
                });
            } else {
                $("#product-search, #product_create").hide();
                $("#sell-existent-product").load(Routing.generate('create_from_product', {company_id: company_id, product_upc: $(this).attr("data-id")}));
                $("#sell-existent-product").show();
            }
        },
        error: function() {
            request.abort();
        }
    });
});

单击此HTML代码时调用代码:

<button data-id="00000000000" class="btn sell-product">Sell</button> 

这是来自Symfony控制器的代码:

/**
 * Check if the current company has a stock created for the product
 * @Route("/stock_exists/{company_id}/{product_upc}", name="stock_exists", requirements={"company_id" = "\d+", "product_upc" = "\d+"})
 * @Method("GET")
 */
public function StockExistsAction($company_id, $product_upc) {
    $em = $this->getDoctrine()->getManager();

    $result = array();
    $result['response'] = true;

    if (!$company_id || !$product_upc) {
        $result['response'] = false;
        $result['msg'] = 'Wrong parameters';
    }

    $product = $em->getRepository('ProductBundle:Product')->find($product_upc);
    $company = $em->getRepository('CompanyBundle:Company')->find($company_id);

    if (!$product || !$company) {
        $result['response'] = false;
        $result['msg'] = 'Error not found product or company';
    }

    $stock = $em->getRepository('StockBundle:KStock')->findBy(array('company' => $company_id, 'product' => $product_upc));
    if ($stock) {
        $result['response'] = false;
        $result['msg'] = 'This company has a stock created for this product. Only one stock per product is allowed.';
    }

    return new JsonResponse($result);
}

/**
 * Display a form to create new stock
 * 
 * @Route("/existent/{company_id}/{product_upc}", name="create_from_product", requirements={"company_id" = "\d+", "product_upc" = "\d+"})
 * @Method("GET")
 */
public function newExistsAction($company_id, $product_upc) {
    $em = $this->getDoctrine()->getManager();
    $entity = new KStock();

    $product = $em->getRepository('ProductBundle:Product')->find($product_upc);
    $company = $em->getRepository('CompanyBundle:Company')->find($company_id);

    $form = $this->createForm(new KStockType($company->getId(), $product->getUpc()));
    return $this->render('StockBundle:Stock:stock_existent_product.html.twig', array('entity' => $entity, 'form' => $form->createView(), 'company' => $company_id, 'product' => $product_upc));
}

UPC =“00000000000”的产品存在但不适用于已登录的公司,因此它会呈现视图并发送输出,如上图所示:

enter image description here

这是我正在加载的视图:

<div>
    <div id="stock_container_form">        
        <link href="{{ asset('/bundles/stock/css/foundation-datepicker.css') }}" rel="stylesheet" />
        {% if edit %}
            <input type="hidden" name="_method" value="PUT" />
        {% endif%}

        <div class="large-12 columns">
            <label>{{ form_label(form.sku) }}</label>
            {{ form_errors(form.sku) }}
            {{ form_widget(form.sku) }}
        </div>
        <div class="large-12 columns">
            <label>{{ form_label(form.price) }}</label>
            {{ form_errors(form.price) }}
            {{ form_widget(form.price) }} {{ form_widget(form.unit) }}
        </div>
        <div class="large-12 columns">
            <label>{{ form_label(form.amount) }}</label>
            {{ form_errors(form.amount) }}
            {{ form_widget(form.amount) }}
        </div>
        <div class="large-12 columns">
            <label>{{ form_label(form.status) }}</label>
            {{ form_errors(form.status) }}
            {{ form_widget(form.status) }}
        </div>
        <div class="large-12 columns">
            <label>{{ form_label(form.condition) }}</label>
            {{ form_errors(form.condition) }}
            {{ form_widget(form.condition) }}
        </div>
        <div class="large-12 columns">
            {{ form_label(form.width) }} {{ form_widget(form.width) }}
            {{ form_label(form.height) }} {{ form_widget(form.height) }}
            {{ form_label(form.length) }} {{ form_widget(form.length) }}
            {{ form_widget(form.nlength) }}
        </div>
        <div class="large-12 columns">
            {{ form_label(form.weight) }} {{ form_widget(form.weight) }}
            {{ form_widget(form.nweight) }}
        </div>
        <div class="large-12 columns">
            <label>{{ form_label(form.description) }}</label>
            {{ form_errors(form.description) }}
            {{ form_widget(form.description) }}
        </div>
        <div class="large-12 columns">
            <label>{{ form_label(form.start_date) }}</label>
            {{ form_errors(form.start_date) }}
            {{ form_widget(form.start_date) }}
        </div>
        <div class="large-12 columns">
            <label>{{ form_label(form.warranty) }}</label>
            {{ form_errors(form.warranty) }}
            {{ form_widget(form.warranty) }}
        </div>
        <div class="large-12 columns">
            <label>{{ form_label(form.valid_time) }}</label>
            {{ form_errors(form.valid_time) }}
            {{ form_widget(form.valid_time) }}
        </div>

        {{ form_rest(form) }}   
    </div>
    <div class="record_actions">
        <button type="button" id="create_stock">{{'Crear'|trans}}</button>
    </div>
</div> 

<script src="{{ asset('/bundles/stock/js/foundation-datepicker.js') }}" type="text/javascript"></script>
<script src="{{ asset('/bundles/stock/js/common.js') }}" type="text/javascript"></script>

任何想法在这里有什么问题?我被困了,无法找到问题所在的地方

更新

这是一张图片,显示我拨打电话后生成的代码(注意:我已经修复了按钮类型)

enter image description here

1 个答案:

答案 0 :(得分:0)

一次读完我的代码后,我发现了我的错误。看到这段代码:

$(".sell-product").click(function() {
    var request = $.ajax({
        type: 'GET',
        url: Routing.generate('stock_exists', {company_id: company_id, product_upc: $(this).attr("data-id")}),
        success: function(data) {
            $("#layout-center").html();
            if (data.response === false) {
                $.prompt(data.msg, {
                    title: "Este producto ya posee stock, quiere actualizarlo?",
                    buttons: {"Sí": true, "No": false},
                    submit: function(e, v, m, f) {
                        if (v === true) {
                            // redireccionamos a otro lado
                        }
                    }
                });
            } else {
                $("#product-search, #product_create").hide();
                $("#sell-existent-product").load(Routing.generate('create_from_product', {company_id: company_id, product_upc: $(this).attr("data-id")}));
                $("#sell-existent-product").show();
            }
        },
        error: function() {
            request.abort();
        }
    });
});

如果您在此行注意到:

$("#sell-existent-product").load(Routing.generate('create_from_product', {company_id: company_id, product_upc: $(this).attr("data-id")}));

我使用的$(this)在该上下文中不存在,因此我在外部创建var然后访问该内部的var。请参阅包含修复程序的代码:

$(".sell-product").click(function() {
    var current = $(this);
    var request = $.ajax({
        type: 'GET',
        url: Routing.generate('stock_exists', {company_id: company_id, product_upc: current.attr("data-id")}),
        success: function(data) {
            $("#layout-center").html();
            if (data.response === false) {
                $.prompt(data.msg, {
                    title: "Este producto ya posee stock, quiere actualizarlo?",
                    buttons: {"Sí": true, "No": false},
                    submit: function(e, v, m, f) {
                        if (v === true) {
                            // redireccionamos a otro lado
                        }
                    }
                });
            } else {
                $("#product-search, #product_create").hide();
                $("#sell-existent-product").load(Routing.generate('create_from_product', {company_id: company_id, product_upc: current.attr("data-id")}));
                $("#sell-existent-product").show();
            }
        },
        error: function() {
            request.abort();
        }
    });
});

请注意这部分var current = $(this);,希望这有助于另一部分!!