简体   繁体   English

未捕获的ReferenceError:未定义user_id

[英]Uncaught ReferenceError: user_id is not defined

what i Need 我需要的

  • I Need to call function from href . 我需要从href调用函数。

html code HTML代码

 <a href="#" onclick="peopleattending('.$_COOKIE['user'].', '.$_COOKIE['evnt_id'].');">

js code js代码

         <script>

        $(document).ready(function()
        {

            function peopleattending(user_id,event_id)
            {

                console.log(user_id);
                console.log(event_id);

                 $.ajax({
                    type:"GET",
                    url:"{{get_hash_attend(user_id,event_id) }}", 
                     success: function(data)
                     {
                           $("#attend").text("Attending");



                    }
                });

            }

             peopleattending(user_id,event_id);


         });

        </script>

problem 问题

  • problem Uncaught ReferenceError: user_id is not defined in peopleattending(user_id,event_id); 问题未捕获的ReferenceError:在peopleattending(user_id,event_id)中未定义user_id;
  • though im getting values say user_id=112223 and event_id=9. 尽管即时获取值说user_id = 112223和event_id = 9。
  • where im wrong if passing values in function in url : get_hash_attend(user_id,event_id) same error appears. 如果在url中的函数中传递值时出现错误:get_hash_attend(user_id,event_id)出现相同的错误。

user_id and event_id are only defined as parameters within your peopleattending function. user_idevent_id仅在人员peopleattending功能中定义为参数。 when you call the code like this: 当您这样调用代码时:

peopleattending(user_id,event_id);

Your basically saying 你基本上是说

peopleattending(undefined,undefined);    

You could set your variables on pageload within your javascript block instead of within your anchor tag's function reference 您可以在javascript块中的pageload上设置变量,而不是在锚标记的函数参考中设置变量

<script>
    var _user_id = '<?php $_COOKIE['user'] ?>';
    var _event_id = '<?php $_COOKIE['evnt_id'] ?>';

    $(document).ready(function()
    {
        function peopleattending(user_id,event_id)
        {
             console.log(_user_id);
             console.log(_event_id);

             $.ajax({
                type:"GET",
                url:"{{get_hash_attend(user_id,event_id) }}", 
                success: function(data)
                {
                       $("#attend").text("Attending");
                }
            });

        }

        peopleattending(_user_id,_event_id);

     });
</script>

And then your anchor tag would look like this 然后您的锚标签看起来像这样

<a href="#" onclick="peopleattending(_user_id,_event_id);">

For Symfony: 对于Symfony:

<script>
    var _user_id = '{% app.request.cookies.get('user') %}';
    var _event_id = '{% app.request.cookies.get('event_id') %}';

I note there was a typo in your question, evnt_id instead of event_id . 我注意到您的问题中有一个错别字, evnt_id而不是event_id If this typo is also in your code then you'll need to fix it or change it in the javascript 如果您的代码中也有此错字,那么您需要对其进行修复或在javascript中进行更改

If your vars are numbers and not strings, you can remove the quotes 如果您的变量是数字而不是字符串,则可以删除引号

<script>
    var _user_id = {% app.request.cookies.get('user') %};
    var _event_id = {% app.request.cookies.get('event_id') %};

If you want to use JQuery to read the cookies directly you can use the Cookie plugin for jQuery 如果您想使用JQuery直接读取Cookie,则可以使用jQueryCookie插件

<script>
    var _user_id = $.cookie("user");
    var _event_id = $.cookie("event_id");

Although at this point you could drop the variables and call $.cookie("user") directly anywhere its needed. 尽管此时您可以删除变量,然后在需要的任何地方直接调用$.cookie("user")

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

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