AJAX继续调用同一页面的URL,而不是API URL

时间:2018-08-09 09:30:37

标签: php symfony jquery-select2

我正在使用 Symfony版本3。* ,并且正在使用 tetranz / select2entity-bundle Select2 选择要使用的资产分配给用户。

由于某些原因,当尝试从API URL获取JSON输出时,AJAX会继续调用相同的页面URL。

URL应该是:

  

http://127.0.0.1:8000/asset/api/asset_list

但是使用的URL是:

  

http://127.0.0.1:8000/user/1?field_name=assets

/**
 * Lists all asset entities.
 *
 * @Route("/api/asset_list", name="api_asset_list")
 * @Method({"GET", "POST"})
 */
public function apiAssetListAction()
{
    $em = $this->getDoctrine()->getManager();
    $assets = $em->getRepository('AppBundle:Asset')->findAll();

    foreach($assets as $asset)
            $itemArray[] = array('id' => $asset->getId(),'text' => $asset->__toString(),);

    return new Response(json_encode($itemArray), 200, array('Content-Type' => 'application/json'));
}



$builder->add('assets', Select2EntityType::class, [
                    'multiple' => true,
                    'remote_route' => 'api_asset_list',
                    'class' => 'AppBundle::Asset',
                    // 'primary_key' => 'id',
                    'text_property' => 'code',
                    'minimum_input_length' => 2,
                    'page_limit' => 10,
                    'allow_clear' => true,
                    'delay' => 250,
                    'cache' => true,
                    'cache_timeout' => 60000, // if 'cache' is true
                    'language' => 'en',
                    'placeholder' => 'Select assigned assets',
                    // 'object_manager' => $objectManager, // inject a custom object / entity manager 
                ]);

我的Twig代码是如此简单,因为我正在使用Symfony的Form Builder为我完成工作:

 <div class = "modal" id = "assignModal" tabindex="-1" role = "dialog">
    <div class = "modal-dialog" role = "document">
        <div class = "modal-content">
            <div class = "modal-header">
                <h5 class = "modal-title">Assign Assets</h5>
            </div>
            <div class = "modal-body">
                {{form_start(assign_form)}}
                {{form_widget(assign_form)}}
                <input class = "btn btn-block btn-success" type="submit" value="Assign" />
                {{form_end(assign_form)}}
            </div>
        </div>
    </div>
</div> 

我还在我的twig:form_themes配置中包括了该模板:

  

TetranzSelect2EntityBundle:Form:fields.html.twig

我使用的JS脚本列表:

<script src="/js/jQuery.js"></script>
<script src="/js/jquery.collection.js"></script>
<script src="/js/popper.js"></script>
<script src="/js/bootstrap.js"></script>
<script src="/js/datatables.js"></script>
<script src="/js/list.js"></script>
<script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script>
<script src="/js/main.js"></script>
<script src="/js/select2.js"></script>
<script src="/bundles/tetranzselect2entity/js/select2entity.js"></script>

希望你们能提供帮助。

0 个答案:

没有答案