当前位置: 代码迷 >> JavaScript >> jquery 中的ajax+json 无刷新交付
  详细解决方案

jquery 中的ajax+json 无刷新交付

热度:450   发布时间:2012-07-26 12:01:08.0
jquery 中的ajax+json 无刷新提交
js代码
function timerFreq()
{
var ss = Math.random();
// 实现页面无刷新调用timer方法
$.ajax(
{
type : 'POST',
url : path + '/alltimer/timerFreq',
data : 'id=' + ss + "&type=" + type,
success : function(msg)
{
var data = JSON.parse(msg);

// 循环
$.each(data, function(k, v)
{
if ("intra" == type)
{
$("#" + v['hnbIdentifier'] + v['intraCid']).html(v['status']);
}
else
{
$("#" + v['hnbIdentifier'] + v['interCid']).html(v['status']);
}

if ("离线" == v['status'])
{
$(("#" + v['hnbIdentifier'] + v['intraCid'])).css("color", "red");
$(("#" + v['hnbIdentifier'] + v['interCid'])).css("color", "red");
}
else
{
$(("#" + v['hnbIdentifier'] + v['intraCid'])).css("color", "black");
$(("#" + v['hnbIdentifier'] + v['interCid'])).css("color", "black");
}
});
},
error : function(msg, textStatus, e)
{
window.location = path + "/login.jsp";
}
});

// 定时器:每20秒访问下数据库
window.setTimeout(timerFreq, 20000);
}

jsp代码:
<c:if test="${type != null }">
                <c:if test="${type eq 'intra' }">
                   <td id="${freq.hnbIdentifier }${freq.intraCid}"></td>
                </c:if>
                <c:if test="${type eq 'inter' }">
                   <td id="${freq.hnbIdentifier }${freq.interCid}"></td>
                </c:if>
                </c:if>

java后台代码:

  @RequestMapping("/timerFreq")
    public void timerIntraFreq(HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
            //从request范围内取type的值,用来判断同频和异频小区
            String type = request.getParameter("type");
           
            //初始化timerList对象
            List<TimerList> timerList = null;
           
            //初始化变量
            String interFreqID = "";
            String intraFreqID = "";
            String intraCid = "";
            String interCid = "";
           
            //初始化StringBuffer对象
            StringBuffer sb = new StringBuffer();
           
            //根据类型判断是同频小区还是异频小区,类型是intra是同频小区,否则是异频小区
            if ("intra".equals(type))
            {
                //得到基站标识和状态的集合
                timerList = timerService.getIntraFreqCellTimer();
            }
            else
            {
                timerList = timerService.getInterFreqCellTimer();
            }
            //得到集合的长度
            int size = timerList.size();
           
            int i = 0;
           
            //开始拼sb字符串
            sb.append("[");
           
            //便利集合取得相对应的值存入StringBuffer sb
            for (TimerList timerList2 : timerList)
            {
                //循环一次i自加1
                i++;
                String hnbIdentifier = timerList2.getHnbIdentifier();
                String statu = timerList2.getStatus();
                String internetID = timerList2.getInternetID();
                String status = null;
               
                if (null == statu || "".equals(statu))
                {
                    status = "离线";
                }
                else if (statu.equals("1"))
                {
                    status = "在线";
                }
                else
                {
                    status = "离线";
                }
               
                //判断同频小区和异频小区,如果type="intra"是同频小区否则是异频小区
                if ("intra".equals(type))
                {
                    if (null != timerList2.getIntraFreqID())
                    {
                        intraFreqID = timerList2.getIntraFreqID();
                    }
                    else
                    {
                        intraFreqID = "";
                    }
                    if (null != timerList2.getIntraCid())
                    {
                        intraCid = timerList2.getIntraCid().toString();
                    }
                    else
                    {
                        intraCid = "";
                    }
                }
                else
                {
                    if (null != timerList2.getInterFreqID())
                    {
                        interFreqID = timerList2.getInterFreqID();
                    }
                    else
                    {
                        interFreqID = "";
                    }
                   
                    if (null != timerList2.getInterCid())
                    {
                        interCid = timerList2.getInterCid().toString();
                    }
                    else
                    {
                        interCid = "";
                    }
                }
               
                sb.append("{\"hnbIdentifier\":\"");
                sb.append(hnbIdentifier);
                sb.append("\",\"status\":\"");
                sb.append(status);
                sb.append("\",\"internetID\":\"");
                sb.append(internetID);
                sb.append("\",\"intraFreqID\":\"");
                sb.append(intraFreqID);
                sb.append("\",\"intraCid\":\"");
                sb.append(intraCid);
                sb.append("\",\"interFreqID\":\"");
                sb.append(interFreqID);
                sb.append("\",\"interCid\":\"");
                sb.append(interCid);
                sb.append("\"}");
               
                //如果i小于size字符串sb中加","
                if (i < size)
                {
                    sb.append(",");
                }
            }
           
            sb.append("]");
           
            //拼好的字符串赋值给变量
            String json = sb.toString();
           
            //定义输出变量
            PrintWriter out = response.getWriter();
            out.write(json);
            out.flush();
            out.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
            logger.error(e);
        }
    }
  相关解决方案