JSON 元素为 null,导致“Uncaught TypeError:”对于 JQote2
背景:我的页面上有两个下拉菜单,当它们发生更改时,我使用 AEFXX 的 JQote 插件来使用一些返回的 JSON 数据填充表。在 JSON 响应中,一些用户故事具有与其关联的任务,但这些任务没有太多信息,因此我必须为每个任务执行额外的 GET 请求才能返回此信息。返回的某些任务没有分配所有者,导致字段 Task.Owner = null。
以下是我用来获取每个任务的代码:
if(result.Tasks != ""){
$.each(result.Tasks, function(i, tasks){
console.log(tasks._ref);
$.ajax({
url: tasks._ref,
dataType: 'jsonp',
jsonp: 'jsonp',
success: function(data, textStatus, jqXHR) {
$.get("task.tpl", function(tmpl) {
$("#reqs").jqoteapp(tmpl, data.Task);
});
}
});
})
}
模板 (task.tpl) 如下所示:
<tr id="infoReturned">
<td name="id" class="row"><%= this.FormattedID %></td>
<td name="name" class="row"><%= this._refObjectName %></td>
<td name="state" class="row"><%= this.State %></td>
<td name="owner" class="row"><%= this.Owner._refObjectName %></td>
</tr>
试用: 我提到了 如何测试空对象JSON.
我添加了以下函数:
function isEmpty(obj) {
for (var prop in obj) {
if (obj.hasOwnProperty(prop))
console.log("false");
return false;
}
}
,将 GET 的成功函数修改为:
success: function(data, textStatus, jqXHR) {
if (isEmpty(data.Task.Owner)){
$.get("emptyTask.tpl", function(tmpl) {
$("#reqs").jqoteapp(tmpl, data.Task);
});
}
else {
$.get("task.tpl", function(tmpl) {
$("#reqs").jqoteapp(tmpl, data.Task);
});
}
}
,并创建了一个新的模板文件(emptyTask.tpl),其中 Owner 部分为空:
<tr id="infoReturned">
<td name="id" class="row"><%= this.FormattedID %></td>
<td name="name" class="row"><%= this._refObjectName %></td>
<td name="state" class="row"><%= this.State %></td>
<td name="owner" class="row"></td>
</tr>
,但我仍然收到错误:
Uncaught TypeError: <unknown message TemplateExecutionError>
(anonymous function)
$.extend.jqotejquery.jqote2.js:107
$.each.$.fn.(anonymous function)jquery.jqote2.js:76
$.ajax.success.$.each.$.each.$.ajax.successpy2Test.html:155
jQuery.extend._Deferred.deferred.resolveWithjquery-latest.js:1016
donejquery-latest.js:7247
jQuery.ajaxTransport.send.callbackjquery-latest.js:8028
问题: 如何修复 task.tpl 的最后部分以考虑可能的空值,或修改 AJAX 成功函数以正确处理这种情况?
Background: I have two dropdowns on my page and when they are changed, I am using the JQote plugin by AEFXX to populate a table with some of the returned JSON data. In the JSON response, some of the User Stories have Tasks associated with them, but these tasks do not have much information with them, so I had to do an extra GET request for each of the Tasks to return this information. Some of the tasks which are returned do not have an Owner assigned, resulting in the field Task.Owner = null.
Here is the code that I'm using to get each of the tasks:
if(result.Tasks != ""){
$.each(result.Tasks, function(i, tasks){
console.log(tasks._ref);
$.ajax({
url: tasks._ref,
dataType: 'jsonp',
jsonp: 'jsonp',
success: function(data, textStatus, jqXHR) {
$.get("task.tpl", function(tmpl) {
$("#reqs").jqoteapp(tmpl, data.Task);
});
}
});
})
}
The template (task.tpl) looks like this:
<tr id="infoReturned">
<td name="id" class="row"><%= this.FormattedID %></td>
<td name="name" class="row"><%= this._refObjectName %></td>
<td name="state" class="row"><%= this.State %></td>
<td name="owner" class="row"><%= this.Owner._refObjectName %></td>
</tr>
Trial:
I referred to How to test an empty Object from JSON.
I added the following function:
function isEmpty(obj) {
for (var prop in obj) {
if (obj.hasOwnProperty(prop))
console.log("false");
return false;
}
}
, modified the success function of the GET to be this:
success: function(data, textStatus, jqXHR) {
if (isEmpty(data.Task.Owner)){
$.get("emptyTask.tpl", function(tmpl) {
$("#reqs").jqoteapp(tmpl, data.Task);
});
}
else {
$.get("task.tpl", function(tmpl) {
$("#reqs").jqoteapp(tmpl, data.Task);
});
}
}
, and created a new template file (emptyTask.tpl) with the Owner section empty:
<tr id="infoReturned">
<td name="id" class="row"><%= this.FormattedID %></td>
<td name="name" class="row"><%= this._refObjectName %></td>
<td name="state" class="row"><%= this.State %></td>
<td name="owner" class="row"></td>
</tr>
, but I'm still getting the error:
Uncaught TypeError: <unknown message TemplateExecutionError>
(anonymous function)
$.extend.jqotejquery.jqote2.js:107
$.each.$.fn.(anonymous function)jquery.jqote2.js:76
$.ajax.success.$.each.$.each.$.ajax.successpy2Test.html:155
jQuery.extend._Deferred.deferred.resolveWithjquery-latest.js:1016
donejquery-latest.js:7247
jQuery.ajaxTransport.send.callbackjquery-latest.js:8028
Question:
How can I fix the last part of the task.tpl to account for possible null values, or modify the AJAX success function to correctly handle this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到了一个与我之前尝试过的解决方案类似的解决方案:
我仍然对修改 template 文件以处理此问题的解决方案感兴趣。
如果您还有其他答案,请留言!
Found a solution which is similar to what I was trying earlier:
I'm still interested in a solution in which the template file is modified to handle this issue though.
Please post if you have another answer!