未捕获的引用错误: 未定义user_id
Uncaught ReferenceError: user_id is not defined
我需要什么
- 我需要从 href 调用函数。
网页代码
<a href="#" onclick="peopleattending('.$_COOKIE['user'].', '.$_COOKIE['evnt_id'].');">
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>
问题
- 问题未捕获引用错误:user_id未在人员中定义(user_id,event_id);
- 尽管我得到的值说 user_id=112223 和 event_id=9。
- 如果在 url 中的函数中传递值,则 im 错误:get_hash_attend(user_id,event_id) 出现相同的错误。
user_id
和event_id
仅在peopleattending
函数中定义为参数。 当你像这样调用代码时:
peopleattending(user_id,event_id);
你基本上说
peopleattending(undefined,undefined);
您可以在 javascript 块中的页面加载中设置变量,而不是在锚标签的函数引用中设置变量
<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>
然后你的锚标签将看起来像这样
<a href="#" onclick="peopleattending(_user_id,_event_id);">
对于Symfony:
<script>
var _user_id = '{% app.request.cookies.get('user') %}';
var _event_id = '{% app.request.cookies.get('event_id') %}';
我注意到你的问题中有一个错字,evnt_id
而不是event_id
.如果这个拼写错误也在你的代码中,那么你需要在javascript中修复它或更改它
如果您的变量是数字而不是字符串,则可以删除引号
<script>
var _user_id = {% app.request.cookies.get('user') %};
var _event_id = {% app.request.cookies.get('event_id') %};
如果你想使用JQuery直接读取cookie,你可以使用jQuery的Cookie插件。
<script>
var _user_id = $.cookie("user");
var _event_id = $.cookie("event_id");
尽管此时您可以删除变量并直接在需要的任何位置调用$.cookie("user")
。
相关文章:
- 为什么“;未定义的“;在JavaScript中结束循环
- 要求未定义JS回调参数
- 如何检查管道中未定义的项目
- TypeError:无法读取属性'推'未定义的JavaScript
- $window.ga在AngularJS事件中未定义
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 无法获取属性'selectedIndex'的未定义引用或null引用
- 如何消除代码中的未定义和其他问题
- 未捕获的ReferenceError:$未定义
- this.router在AngularJS 2中未定义
- 未捕获的类型错误:无法读取属性'删除'的未定义
- 节点fs.stat名称未定义
- 解析-为什么user.getSessionToken()返回未定义的结果
- req.user在使用express服务器passport.js进行twitter身份验证后未定义
- 访问Passport'React(ES6)中的用户需求?API调用将req.user返回为未定义
- Meteor.user().profile返回'配置文件'未定义
- TypeError: Meteor.user(. .).Services是未定义的
- Meteor.user()在客户端控制器中未定义
- “user.profile” 未定义,尽管它在 Accounts.createUser() 中声明