есть такой сервлет который возращает json
| Код | public class GadgetOssEventServlet extends HttpServlet {
@EJB(name = "OssEventStoreBean") private OssEventStoreLocal alarmStoreBean;
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { JSONArray alarmEventFacilitytListOfJsonArray = null; List<AlarmEventFacility> allAlarms = alarmStoreBean.getAllAlarms(); try { alarmEventFacilitytListOfJsonArray = alarmEventFacilitytListToJsonArray(allAlarms); } catch (JSONException ex) { Logger.getLogger(GadgetOssEventServlet.class.getName()).log(Level.SEVERE, null, ex); } response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); response.setHeader("Cache-Control", "no-cache"); response.getWriter().print(alarmEventFacilitytListOfJsonArray); response.getWriter().flush(); } private JSONArray alarmEventFacilitytListToJsonArray(List<AlarmEventFacility> list) throws JSONException { JSONArray array = new JSONArray(); for (AlarmEventFacility alarmEventFacility : list) { JSONObject jsonObject = new JSONObject(); jsonObject.put("eventType", alarmEventFacility.getEventType()); jsonObject.put("firedAt", new Date(alarmEventFacility.getFiredAt())); jsonObject.put("source", alarmEventFacility.getSource()); jsonObject.put("pinName", alarmEventFacility.getPinName()); array.put(jsonObject); } return array; } }
|
на клиенте крутится js который разбирает json по урлу, вот сам скрипт
| Код | function getRequest() { var url = "http://192.168.2.221:8080/OssEventManager-web/gadgetOss.json"; $.getJSON(url, function(data) { var items = [];
$.each(data, function(key, val) { if (val.eventType == "Alarm") items.push('<li>' + '<font size="2" color="red">' + val.eventType + ' ' + val.firedAt + ' '+ val.source + ' ' + val.pinName + '</font>' + '</li>'); else items.push('<li>' + '<font size="2" color="#000080">' + val.eventType + ' ' + val.firedAt + ' '+ val.source + ' ' + val.pinName + '</font>' + '</li>'); });
$('<div/>', { 'class': 'my-new-list', html: items.join('') }).appendTo('body'); } ).error( function() { console.log("ajax error"); } ); }
|
и если в урл указать, на локальный файл json то все выводит ок, а если на удаленный сервер, в консоль выводит ошибку если в браузер вбить урл, то он предлагает сохранить файл, не могу понять в чем проблема |