开发者

Ajax - getting separate lines of data

i would like to ask if it is possible in ajax technology (working with servlets) to get not the whole data from responseText but line by line. i mean when puttin开发者_高级运维g in servlet lines of text using println method (response object) i would like to get every seperate line on the client side (for displaying achieving data like in console)


The AJAX responses are sent as a single unit from the server to the client - so no, you cannot read them in real-time (as and when the server side code calls println). But you can easily emulate it by splitting the response string at new lines - response.split("\n") - and iterating through the resulting array.


var yourLines = yourString.split("\n");
for (var i = 0, j = yourLines.length; i < j; i++) {
  var currentLine = yourLines[i];
  /* … */
}


This is just possible. It more look like that you have trouble with splitting the lines in Javascript. The println() writes to the response with the system default line separator which is usually \r\n. So if you want to get the separate lines in Javascript, you need to split the responseText on \r\n to get an array of lines.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜