我是JSON / AJAX的新手 我在从服务器上获取的JSON对象中显示数据时遇到了一些问题。 网址“http:// localhost:8387 / rest / resourcestatus.json”表示此对象,我想通过HTML / Javascript显示该对象。此对象存储一些监控信息:
{"groupStatus":[ {"id":"AL Process","time":1332755316976,"level":0,"warningIds":[],"errorIds":[]}, {"id":"AL:instance1","time":1332919465317,"level":0,"warningIds":[],"errorIds":[]}, {"id":"AL:instance2","time":1332919465317,"level":1,"warningIds":["documentarea.locked"],"errorIds":[]}, {"id":"SL","time":1331208543687,"level":0,"warningIds":[],"errorIds":[]} ]}
由于请求的URL与我的域不同,我无法创建典型的XMLHttpRequest。所以我发现有一个AJAX跨域请求可以通过jQuerys“getJSON()”方法实现。
我想在表格中显示 ID 及其级别。
实现这一目标的任何解决方案?
答案 0 :(得分:0)
$.getJSON的回调函数包含AJAX调用的结果。
$.getJSON('http://localhost:8387/rest/resourcestatus.json', function(data) {
$(data.groupStatus).each(function() {
// do something with $(this).id
});
});
答案 1 :(得分:0)
我认为你指的是JSONP。见jQuery.ajax例如:
var url = 'http://localhost:8387/rest/resourcestatus.json';
$.getJSON(url+'?callback=?', function(data)
{
//data is
/*{
"groupStatus":
[
{"id":"AL Process","time":1332755316976,"level":0,"warningIds":[],"errorIds":[]},
{"id":"AL:instance1","time":1332919465317,"level":0,"warningIds":[],"errorIds":[]},
{"id":"AL:instance2","time":1332919465317,"level":1,"warningIds":["documentarea.locked"],"errorIds":[]},
{"id":"SL","time":1331208543687,"level":0,"warningIds":[],"errorIds":[]}
]
}*/
});
在服务器端,您需要将响应包装到JavaScript函数中:response = Request["callback"] +"("+ response+")";
结果将如下所示:
?({"groupStatus":[{"id":"AL ....})
因此浏览器实际上会加载一个有效的java脚本代码。