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

I'm using Symfony version 3.* and I am using tetranz/select2entity-bundle to have Select2 to select assets to be assigned to users.

For some reason, AJAX keeps on calling the same page URL when trying to get the JSON output from an API URL.

URL should be:

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

But the URL used is:

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 
                ]);

My Twig Code is as simple as this since I am using the Form Builder of Symfony to do the work for me:

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

I also included this template in in my twig:form_themes configuration:

TetranzSelect2EntityBundle:Form:fields.html.twig

My list of JS Scripts used:

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

I hope you guys can help.

dtbi27903
dtbi27903 捆绑应该为我做了。构建器的remote_route属性指定它将使用的URL。
大约 2 年之前 回复
douji1853
douji1853 它可能从表单元素中获取目标URL,并且由于您没有为其明确指定,因此它指向当前URL-?
大约 2 年之前 回复
doubo9799
doubo9799 我没有写它,symfony为我写了代码。我正在使用tetranz/select2entity-bundle自动执行此操作。
大约 2 年之前 回复
dongli4274
matlabmann 你为ajax调用写的代码在哪里..?
大约 2 年之前 回复
dongtuanzi1080
dongtuanzi1080 麦克斯基:按照你的要求完成。
大约 2 年之前 回复
douchui4815
douchui4815 HamzaNig:我得到“无法生成指定路由的URL”{{api_asset_list}}“,因为这样的路由不存在。”错误。
大约 2 年之前 回复
dtlh12053
dtlh12053 尝试使用{{api_asset_list}}
大约 2 年之前 回复
dqf42223
dqf42223 请告诉我们您的twig代码,尤其是javascript部分
大约 2 年之前 回复
douaoli5328
douaoli5328 我对ajax代码并不熟悉,但我能说的是$builder的'remote_route'=>'api_asset_list'有正确的url。
大约 2 年之前 回复
doudi8829
doudi8829 如果您的客户端代码请求错误的URL,为什么不向我们展示客户端代码?
大约 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问