简体   繁体   English

将数据从JS发送到Controller中的函数,并从函数中获取结果

[英]Send data from JS to function in the Controller and get the result from the function

Please I need help I am stuck in this since yesterday, the probleme is I got a numberInput I use jquery autocomplete that helps me select a user's subscription number. 请问我需要帮助我从昨天开始就陷入困境,问题是我得到了一个数字输入我使用jquery自动完成功能来帮助我选择一个用户的订阅号码。 With that number I want to get the full name of a user and show it on the twig. 有了这个号码,我想得到一个用户的全名并在树枝上显示。 I am calling the JS function getInfoOfInput() once my input is changed, these are the JS function that I use: 我输入更改后调用JS函数getInfoOfInput(),这些是我使用的JS函数:

function getInfoOfInput()
    {
        var tutoreSelectionne = document.getElementById("numAdherent");
        var tutore = tutoreSelectionne.value;
        //alert(tutoreSelectionne.value);
        recupName(tutore);

    }
    function recupName(tutoreSelectionne) {
        var DATA = {tutoreSelectionne: tutoreSelectionne};
        var request = $.ajax({
            type: "POST",
            url: "{{ path('paces_statistique_default_getnometprenomtutore') }}",
            data: DATA,
            success: function(data){
                $('#name').html(data);
            }
        });
        return false;

    }

This is the function in my controller : 这是我的控制器中的功能:

  /**
 * get fullName
 * @Secure(roles="ROLE_ADMIN")
 * @Route("/recupFullName/")
 * @param Request $request La requête ajax qui envoie le numAd
 * @return Response $fullnam
 */
public function getNomEtPrenomTutore($request){
    $em = $this->getDoctrine()->getManager();
    $tutoRepo = $em->getRepository(Tutore::class);
    if ($request->isXmlHttpRequest()) {
        $numTutore = $request->request->get('tutoreSelectionne');
        $tutore = $tutoRepo->findOneBy(['username' => $numTutore]);
        $nom = $tutore->getNom();
        $prenom = $tutore->getPrenom();
        $fullName = "$nom $prenom";
        return $this->render("PACESStatistiqueBundle:Default:index.html.twig", array('fullName'=>$fullName));
    }
}

PS: The 'username' is the user's subsription number ^^ I know it's not the good the name to use for it. PS:'用户名'是用户的附属号码^^我知道它的名称不是很好用的。

My input on the twig : 我在树枝上的输入:

<div class="ui-autocomplete-input">
<input type="number" id="numAdherent" value="" onchange="getInfoOfInput();"  onkeyup="this.onchange();"  oninput="this.onchange();" onautocomplete="this.onchange();">
</div>

I use this to override the value of the input when I change it 当我改变它时,我用它来覆盖输入的值

// INPUT VALUE OVERRIDE
    $(function () {

        {% if is_granted('ROLE_ADMIN') %}
        $('td input[id^=numAdherent]').each(
                function (){
                    $(this).on("change paste keyup", function()
                    {$(this).attr('value', $(this).val())})
                }
        );
        {% endif %}


    });

this is the error that I get In JS with "Inspect Element" 这是我用JS“Inspect Element”得到的错误

jquery-1.11.3.min.js:5 POST http://localhost:8000/recupFullName/    500(Internal Server Error)
send @ jquery-1.11.3.min.js:5
ajax @ jquery-1.11.3.min.js:5
recupName @ vueStatistique:231
getInfoOfInput @ vueStatistique:226
onchange @ vueStatistique:276
oninput @ vueStatistique:276

Thank you in advance for any help you can give me :) 提前感谢您的任何帮助,你可以给我:)

Internal Server Error normally means, that something is not right in your controller action. 内部服务器错误通常意味着控制器操作中的某些内容不正确。

$tutore = $tutoRepo->findOneBy(['username' => $numTutore]);

This line might be the problem. 这条线可能是问题所在。 For me, username does not sound like a number. 对我来说, username听起来不像是一个数字。 Is this line really correct? 这条线真的是对的吗?

Edit: After checking the error message, here is the solution: Change 编辑:检查错误消息后,这是解决方案:更改

public function getNomEtPrenomTutore($request){

to

public function getNomEtPrenomTutore(Request $request){

and make sure that you actually import the Request class. 并确保您实际导入Request类。

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

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