简体   繁体   English

Symfony Ajax请求错误500

[英]Symfony Ajax Request error 500

Hi everyone I come to you because I have a little problem with my ajax script I share my code and tell me if there is a problem thanks for advances, if you have any solution I thank you, the error made This is: 大家好,我来找你,因为我的ajax脚本有点问题我分享我的代码并告诉我是否有问题感谢预付款,如果你有任何解决方案我感谢你,错误发生了这是:

Argument 1 passed to App \ Repository \ ProductRepository :: findBySearch () must be of the string type, object given, called in C: \ wamp64 \ www \ Shop \ src \ Controller \ FrontController.php on line 201

my controller : 我的控制器:

/**
     * @Route("/recherche/", name="search", methods="POST")
     * @param Request $request
     * @return Response
     */
    public function search(Request $request): Response{

        $search = $this->createForm(SearchType::class);
        $search->handleRequest($request);

        if($request->isXmlHttpRequest()) {

           $value = $search['name'];
           $result = $this->getDoctrine()->getRepository(Product::class)->findBySearch($value);

           return new JsonResponse($result);
        }

        return $this->render('inc/search.html.twig', [
            'title' => 'Effectuer une recherche',
            'search' => $search->createView()
        ]);

    }

My repository in which the SQL query is performed : 我在其中执行SQL查询的存储库:

/**
     * @param $value
     * @return string
     * @throws \Exception
     */
    public function findBySearch(string $value) {

        $bool = 1;
        $query = $this->createQueryBuilder('r')
            ->select('r')
            ->orwhere('r.name LIKE :chaine')
            ->orWhere('r.description LIKE :chaine')
            ->andWhere('r.isPublished = :bool')
            ->orderBy('r.createdAt', 'DESC')
            ->setParameter(':chaine', '%'.$value.'%')
            ->setParameter(':bool', $bool)
            ->getQuery();

        try {
            return $query->getResult();
        }
        catch (\Exception $e) {
            throw new \Exception('Problème' . $e->getMessage());
        }
    }

this is my code that allows me to make ajax requests 这是我的代码,允许我发出ajax请求

<!-- Modal Search -->
<div class="modal-search-header flex-c-m trans-04 js-hide-modal-search">
    <div class="container-search-header">
        <button class="flex-c-m btn-hide-modal-search trans-04 js-hide-modal-search">
            <img src="images/icons/icon-close2.png" alt="CLOSE">
        </button>
        <div id="resultat"></div>
         {{ form_start(search, {'method': 'POST', 'attr': {'class': 'wrap-search-header flex-w p-l-15', 'id': 'form'}}) }}
            <button class="flex-c-m trans-04">
                <i class="zmdi zmdi-search"></i>
            </button>
            {{ form_widget(search.name) }}

        {{ form_end(search) }}
    </div>
</div>

<script>

    $(document).ready(function(){

        $("#form").keypress(function(){

            $.ajax({
                type:"POST",
                data: $(this).serialize(),
                url:"{{ path('search') }}",
                success: function(data){
                    $("#search_name").html(data);
                    $.post( "{{ path('search') }}", function( data ) {
                        $( "#resultat" ).html( data );
                    });
                },
                error: function(){
                    $("#search_name").html('Une erreur est survenue.');
                }
            });
            return false;
        });
    });
</script>

Replace 更换

$value = $search['name'];

with

$value = $search->get('name')->getNormData();

Since $search is a FormInterface object, you won't get the submitted value just through $search['name'] . 由于$search是一个FormInterface对象,因此您不会仅通过$search['name']获取提交的值。 You could call $search->getData() to get the whole form data OR $search->get('YOUR_FIELD_NAME')->getNormData() to get an specific value 您可以调用$search->getData()来获取整个表单数据OR $search->get('YOUR_FIELD_NAME')->getNormData()以获取特定值

Also, consider to wrap it with 另外,考虑用它包装

if($search->isSubmitted()) {

   if($request->isXmlHttpRequest()) {
     // ... 
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM