我想我一直在看我的代码太久了。我不断收到错误消息: SCRIPT 1014: Invalid character line 271, character 9 但我找不到错误。
这是代码:
alert('already set');
data = JSON.parse(sessionStorage.ruletypes);
if (data !=null) {
$.each(data, function(i) {
$('#ruletypes')
.append($("<option></option>")
.attr("value",this.id)
.text(this.name));
}); //end .each
}//end if
vdata = JSON.parse(sessionStorage.voicecontacttypes);
if (vdata !=null) {
$.each(vdata, function(i) {
$('#contact_types')
.append($("<option></option>")
.attr("value",this.id)
.text(this.description));
}); //end .each
}//end if
它正在死去的那一行是我为 vdata 赋值的地方:(第 271 行)
vdata = JSON.parse(sessionStorage.voicecontacttypes);
据我所见,vdata 的代码与 data 的代码相同......并且我能够成功填充规则类型选择控件......
任何建议,将不胜感激。谢谢。
编辑 1
我已经更新了将变量设置为如下所示的代码:
$.getJSON(
url = someurl,
function(data) {
sessionStorage.setItem("voicecontacttypes", data);
data = $.parseJSON(data); //converting to a javascript object vs. just string....
console.log("======== voice contact types start =============");
console.log(sessionStorage.getItem("voicecontacttypes"));
console.log("======== voice contact types stop =============");
ETC...
这是 IE 9 的调试窗口中的输出:
LOG: ======== voice contact types start =============
LOG: [{"id":"2","name":"ll","description":"Land Line","objecttype":"3"},{"id":"3","name":"mobile","description":"Mobile Phone","objecttype":"3"},{"id":"6","name":"pager","description":"Pager","objecttype":"2"}]
LOG: ======== voice contact types stop =============
现在检索此数据的代码如下所示:
alert('already set');
console.log("===== getting session data start ======")
data = sessionStorage.getItem("voicecontacttypes");
console.log(data);
console.log("===== getting session data stop ======");
if (data !=null) {
$.each(data, function(i) {
$('#contact_types')
.append($("<option></option>")
.attr("value",this.id)
.text(this.description));
}); //end .each
}//end if
这是控制台中的调试数据:
LOG: ===== getting session data start ======
LOG: [{"id":"2","name":"ll","description":"Land Line","objecttype":"3"},{"id":"3","name":"mobile","description":"Mobile Phone","objecttype":"3"}{"id":"6","name":"pager","description":"Pager","objecttype":"2"}]
LOG: ===== getting session data stop ======
如您所见,我已经实现了使用 get/setItem() 的建议。您可以看到数据在会话中正确保留。但是,现在我收到以下错误消息:
SCRIPT438: Object doesn't support property or method 'text'
这个错误发生在我试图遍历 $('#contact_types') 的地方