0

内容加载成功后,我收到一个奇怪的重定向,我找不到导致此行为的原因,因此我寻求有关此问题的一些帮助或提示。看到这是我正在使用的 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" 的产品存在,但不用于登录的公司,因此它呈现视图并发送输出,如上图所示:

在此处输入图像描述

这是我正在加载的视图:

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

有什么想法吗?我被卡住了,找不到问题/问题出在哪里

更新

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

在此处输入图像描述

4

1 回答 1

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)which 在该上下文中不存在,因此我创建了一个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);,希望这对其他人有帮助!

于 2013-09-01T22:31:04.363 回答