①将object实体转化成json字符串传递: JSON.stringify(obj)
②将json字符串转化成JSON对象值:JSON.parse(json)
实际应用:
javascript
<div id="div_notice" style='font-size:14px; width:100%; height:200px; overflow-y:auto;'></div>
<script>
$(function () {
//初始化
notice();
//定时刷新
setInterval(function () {
notice();
}, 30 * 1000);//30秒
});
//获取notice
function notice() {
$("#div_notice").empty();
$.ajax({
async: false,
type: "POST",
url: "/WorkNotice/GetWorkNoticeList",
data: {},
success: function (result) {
//console.log(result.data);
for (var i = 0; i < result.data.length; i++) {
//①将object实体转化成json字符串传递: JSON.stringify(obj)
var html = `<a onclick=notice_click('` + JSON.stringify(result.data[i]) +`')>` + result.data[i]["MESSAGE"] + ` ` + result.data[i]["CREATED_TIME"] + `</a><br>`;
$("#div_notice").append(html);
}
}
});
}
//点击notice
function notice_click(json) {
console.log(json);
//②将json字符串转化成JSON对象值:JSON.parse(json)
var obj = JSON.parse(json);
if (obj) {
var href = obj.HREF;
$.ajax({
async: false,
type: "POST",
url: "/WorkNotice/WorkNoticeClick",
data: {
"USER_ID": obj.USER_ID,
"COMPANY_UUID": obj.COMPANY_UUID,
"ACCOUNTID": obj.ACCOUNTID,
"TYPE": obj.TYPE,
"BILLCODE": obj.BILLCODE,
},
success: function (result) {
//console.log(result.data);
window.location.href = href;
}
});
}
}
</script>