https://jsfiddle.net/jeeyi345/uchhjz7L/3/ I want to connect a JSP web service I built and return parsed elements, as follows, but the result return slowly.
When I parsed only 1 element the page response a lot faster, but I don't know why when I parsed them all, maybe like a few minutes or longer.
There is only a line of JSON array from the JSP page, like the following:
[{"UID":11,"UrlDate":"2016-10-24","UrlTime":"15:00:00","Drug":2,"AttLevel":43.2864439246683,"AttLevel_Lab":"普通","Allpoint":5,"PVTRuleBase":2,"MemLevel":87}]
Is there something wrong with my code? Thanks in adavance.
// https://jsfiddle.net/vobmzdgr/7/
function json_decode(json) {
return $.parseJSON(json);
}
function json_format(json) {
return JSON.stringify(json_decode(json), null, 2);
}
function json_highlight(json) {
/*
pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }
.string { color: green; }
.number { color: darkorange; }
.boolean { color: blue; }
.null { color: magenta; }
.key { color: red; }
*/
if (typeof json != 'string') {
json = JSON.stringify(json, undefined, 2);
}
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function(match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'color: red;'; //key
} else {
cls = 'color: green;'; //string
}
} else if (/true|false/.test(match)) {
cls = 'color: blue;'; //boolean
} else if (/null/.test(match)) {
cls = 'color: magenta;'; //null
}
return '<span style="' + cls + '">' + match + '</span>';
});
}
function json_AttLevel(json) {
var obj = json_decode(json);
return obj[0].AttLevel;
}
function json_AttLevel_Lab(json) {
var obj = json_decode(json);
return obj[0].AttLevel_Lab;
}
function json_PVTRuleBase(json) {
var obj = json_decode(json);
return obj[0].PVTRuleBase;
}
function json_Drug(json) {
var obj = json_decode(json);
return obj[0].Drug;
}
function json_Allpoint(json) {
var obj = json_decode(json);
return obj[0].Allpoint;
}
$(document).ready(function() {
// var URL="http://140.138.77.151:8080/personalAssementNEW";
var URL = "http://140.138.77.151:8080/FallRiskAPI/getInputData.jsp?ID=11&DATEINFO=2016-10-24&TIMEINFO=15:00:00";
$.ajaxPrefilter(function(options) {
if (options.crossDomain && jQuery.support.cors) {
var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;
}
});
setInterval(function() {
$.get(URL, function(response) {
//原內容
$("#output").text(response);
var bodydata = response;
bodydata = $("<p>" + bodydata + "</p>").text();
var jdata = json_format(bodydata);
$("#output_data").html("<pre>" + json_highlight(jdata) + "</pre>");
$("#output_AttLevel").html("<pre>" + json_AttLevel(jdata) + "</pre>");
$("#output_AttLevel_Lab").html("<pre>" + json_AttLevel_Lab(jdata) + "</pre>");
$("#output_PVTRuleBase").html("<pre>" + json_PVTRuleBase(jdata) + "</pre>");
$("#output_Drug").html("<pre>" + json_Drug(jdata) + "</pre>");
$("#output_Allpoint").html("<pre>" + json_Allpoint(jdata) + "</pre>");
});
// alert("reloaded");
}, 1500);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- https://jsfiddle.net/vobmzdgr/7/ -->
<script language="javascript" src="http://3wa.tw/inc/javascript/jquery/jquery-1.11.3.min.js"></script>
收到的內容:<br>
<div id="output"></div>
<hr>
解算的內容:<br>
<div id="output_data"></div>
專注力等級:<div id="output_AttLevel_Lab"></div>
專注力測驗等級:<div id="output_PVTRuleBase"></div>
用藥分數:<div id="output_Drug"></div>
整體分數:<div id="output_Allpoint"></div>
</div>