日期:2014-05-16  浏览次数:20327 次

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();