我有以下代码:
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>
<table class="table" id="table_id">
<thead>
<th data-dynatable-column="pt_name">Name</th>
<th data-dynatable-column="unit">Unit</th>
<th data-dynatable-column="room">Room</th>
<th data-dynatable-column="fin">FIN</th>
<th data-dynatable-column="line_type">Line Type</th>
<th data-dynatable-column="line_loc">Location</th>
<th data-dynatable-column="line_days">Est Days in Place</th>
<th data-dynatable-column="insert_date">Insertion Date</th>
<th data-dynatable-column="last_dsg_change">Last Dsg Change</th>
<th data-dynatable-column="hosp_insertion">Hospital Insert</th>
<th data-dynatable-column="reason">Reason</th>
</thead>
<tbody></tbody>
</table>
<script src="//cdnjs.cloudflare.com/ajax/libs/json2/20130526/json2.min.js"></script>
<script type="text/javascript" charset="utf8" src="js/jquery-1.11.1.min.js"></script>
<script src="libs/jquery-dynatable/jquery.dynatable.js"></script>
<script>
$(function(){
$.get("model/20_mp_cc_get_cvcs.json",function (data) {
$('#table_id').dynatable({
dataset: {
records: data.cvc_list.qual
}
});
})
});
</script>
</body>
</html>20_mp_cc_get_cvcs.json的一部分是:
{
"cvc_list": {
"cvc_cnt": 12,
"patient_cnt": 6,
"qual": [
{
"dg_id": 20627424964.0,
"enc_id": 82048822.0,
"fin": "700001703",
"hosp_insertion": "y",
"insert_date": "05/29/2014",
"insert_dt_tm": "/date(2014-05-29t00:00:00.000-04:00)/",
"last_dsg_change": "",
"line_days": 31,
"line_loc": "upper",
"line_type": "cvc",
"pers_id": 69935620.0,
"pt_name": "buildtest , domainone",
"reason": "",
"room": "1rmh",
"unit": "1rmh"
},
{
"dg_id": 20627428586.0,
"enc_id": 82048822.0,
"fin": "700001703",
"hosp_insertion": "n",
"insert_date": "05/21/2014",
"insert_dt_tm": "/date(2014-05-21t00:00:00.000-04:00)/",
"last_dsg_change": "",
"line_days": 39,
"line_loc": "rt., brachial",
"line_type": "picc",
"pers_id": 69935620.0,
"pt_name": "buildtest , domainone",
"reason": "",
"room": "1rmh",
"unit": "1rmh"
}
]
}
}我一直收到一个错误,告诉我cvc_list.qual是“空的或者不是对象”。这并不是在所有浏览器中都会发生。就在我们的Citrix VM中,运行文档模式IE7的IE10。我可能做错了什么?
发布于 2014-06-30 12:57:44
$.get可能不能保证JSON解析,最好使用完整版本-
$(function(){
$.ajax({
url: "model/20_mp_cc_get_cvcs.json",
dataType: "json",
type: "get",
success: function(data){
$('#table_id').dynatable({
dataset: {
records: data.cvc_list.qual
}
});
}
});
});IE7中还提供了一个JSON.parse方法,因此您也可以使用该方法在您自己的位置上进行解析。所以你可能会这样做-
$.get("model/20_mp_cc_get_cvcs.json",function (data) {
if (typeof data == "string"){
data = JSON.parse(data);
}
$('#table_id').dynatable({
dataset: {
records: data.cvc_list.qual
}
});
})发布于 2014-06-30 13:23:30
在ajax调用中使用dataType属性。根据jQuery文档,默认情况下它的值是基于guess来评估的;
dataType (默认值:智能猜测(xml、json、script或html))类型: String您期望从服务器返回的数据类型。如果没有指定,jQuery将尝试根据响应的MIME类型推断它( XML MIME类型将生成XML,在1.4JSON中将生成JavaScript对象,在1.4中脚本将执行脚本,其他任何内容都将作为字符串返回)。
如果需要来自服务器端的dataType,请在ajax调用中指定json : json。
或者您也可以使用jQuery.parseJson
Sol#1
$.get("model/20_mp_cc_get_cvcs.json",函数(数据){
$('#table_id').dynatable({
dataset: {
records: data.cvc_list.qual
}
});
}, "json"); // Tell jQuery that you are expecting json.Sol#2
$.get("model/20_mp_cc_get_cvcs.json", function (data) {
var json = jQuery.parseJSON(data); // parse it
$('#table_id').dynatable({
dataset: {
records: json.cvc_list.qual // use the json here
}
});
});https://stackoverflow.com/questions/24482928
复制相似问题