Home  >  Q&A  >  body text

java - ajax 5秒更新数据,返回int型,代码如下,找错

updateCountWx.action里面的代码:

int counttest = we.test(); //counttest 是需要返回的数据
Httpsession session1 = request.getSession();
session1.setAttribute("counttest", counttest);
//最后要返回的数据,我放在了session1里面返回行吗?
out.print(session1)

ajax代码::

    function update() {
        $.ajax({
            url : "updateCountWx.action" ,
            method : "get",
            dataType : "??",
            success:function(data){
                var ??=data;
                var str="";
               str+="<tr><td><font size=\"5\" style=\"微软雅黑\">今天采集数</font>&nbsp;&nbsp;<font size=\"4\">:</font> </td>";
                     str+="<td><font size=\"5\" style=\"微软雅黑\"><B>"+${sessionScope.counttest}+"</B></font>&nbsp;<font size=\"4\" style=\"微软雅黑\">次</font></td></tr>";
                document.getElementById("updatecount").innerHTML=str;
            }
        });
    }

  //页面加载函数    
window.onload = function() {
    setInterval(update, 5000);
}

??的地方应该填什么?
我out.print(session1)对吗,我把返回的int型数据。放在了session1里面。

我需要在ajax里面接收out.print()出来的数据。ajax怎么改

PHP中文网PHP中文网2741 days ago312

reply all(4)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:59:54

    dataType is "JSON", below var jo = data, which is a JavaScript object parsed from JSON.

    If it is int type, it can be output directly, and most cases can be parsed correctly. But it is generally recommended to output an object or array and use a JSON processing library to process it into a string for printing. Common Java JSON libraries include Gson, FastJson, Jackson, etc.

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 09:59:54

    Just process it as ordinary JSON.

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 09:59:54

    Why should I put it in the session and pass it to the front desk? I don't quite understand why you do this. I think it's okay to return the data and format it into a json string in the background and return it to the front desk. After the front desk receives it, it can always json.parse into a json object for access, right?

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 09:59:54

    Great, solved! ! ! Thank you, Master Biancheng, and all my friends who helped me find a solution! ! !

    reply
    0
  • Cancelreply