【问题标题】:jquery template showing Cannot read property 'push' of undefinedjquery 模板显示无法读取未定义的属性“推送”
【发布时间】:2017-01-28 03:40:21
【问题描述】:

您好我正在尝试使用 ajax json 的 jquery 模板,我正在从服务器获取数据,但是当我将该对象传递给模板时,它显示无法读取未定义错误的属性“推送”[错误的图像截图][1]

我已经对此进行了一些研究,但没有得到结果。请大家帮我做同样的事情。

脚本模板

<ul class="entrylist" id="show_application_list">
<script type="text/x-jquery-tmpl" id="show_application_list_template" >
 <li class="even">
 <div class="entry_wrap">
 <div class="entry_content2">
   <h4><a id='${jobid}' class='view_job_details'>${position}</a></h4>
   <big><strong>Applied On:</strong> ${applied_date}</big>
   <big><strong>Status:</strong> 

   {{if scheduled_date}}
   <button id="" class="stdbtn btn_lime show_application_details">Shortlisted</button>
   {{else}}
   <p><big><strong>Under Review</strong></big></p>
   {{/if}}
 </div>
 <br>
 <div class="entry_content2 show_application_details_content" style="display:none">

   <form class="stdform stdform2" method="post" action="#">
     <p>
       <label>Interview On</label>
       <span class="field">
       {{if scheduled_date}}
         ${scheduled_date}
       {{else}}
         <p><big><strong>Under Review</strong></big></p>
       {{/if}}
       </span>
     </p>

     {{if interview_response}}
       <p>
         <label>Interview Response</label>
         <span class="field">${interview_response}</span>
       </p>
       <p>
         <label>Interview Status</label>
         <span class="field">${interview_status}</span>
       </p>
       <p>
         <label>Interview Experience<small>Grade your experience out of 5 <small></label>
         <span class="field"><input type="number" min="0" max="5" name="lastname" id="lastname2" class="longinput"></span>
       </p>
       <p>
         <label>Comment</label>
         <span class="field"><textarea name="lastname" id="lastname2" class="longinput"></textarea></span>
       </p>
       <p class="stdformbutton">
         <button id="${interview_id}" type="button" class="stdbtn btn_lime update_interview_experience">Update Feedback</button>
       </p>
     {{/if}}

   </form>
 </div>
</div>
</li>
</script>               
</ul>

Javascipt 代码

function show_application_employees(){
    $("#applicationlabel").hide();
    $.ajax({
        type:"post",
        url:'functions/functions.php?activity=getapplicationemployees',
        success:function(data)
        {   

            var obj = $.parseJSON(data);
            console.log(obj)
            if(obj.length == 0){
                $("#applicationlabel").show();  
            }else{
                $("#applicationlabel").hide();
                $("#show_application_list").find(".even").remove();
                $("#show_application_list_template").tmpl(obj).appendTo("#show_application_list");
                $(".view_job_details").on("click",show_job_details);
                $(".show_application_details").on("click",show_application_details);
            }
        }
    });

}

更多屏幕截图我的代码在这一点上失败了: error in jquery

error in jquery template js.

【问题讨论】:

  • console.log(obj) 显示什么?
  • 从您的代码中,我只能假设您在 json 中返回一个元素数组,然后您直接尝试在模板中呈现它,而您的模板处理数组中单个元素的呈现。尝试从 obj 渲染一个元素,即 obj [0] 或在模板中为每个元素添加一个。

标签: javascript jquery json jquery-templates


【解决方案1】:

我不知道确切的答案,但在浏览器中进行调试是最好的。您可以在下面看到浏览器开发者控制台。在Sources 选项卡中:

  1. 激活Pause on exceptions 按钮。
  2. 重新运行您的页面,当您遇到抛出错误 (Cannot read property 'push') 的异常时。查看调用堆栈以找出您的代码失败的地方。

或者在success函数中放一个断点,看看你可以单步执行你的代码多远。

问题肯定出在您的代码中,您需要进一步指出问题。一旦你发现你的代码到底在哪里失败了,就发布一个更新。

【讨论】:

    猜你喜欢
    • 2019-03-10
    • 2021-03-06
    • 1970-01-01
    • 1970-01-01
    • 2017-08-12
    • 1970-01-01
    相关资源
    最近更新 更多