ajax 最简单的问题
这是jsp页面
<script language= "javascript ">
var request = false;
try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject( "Msxml2.XMLHTTP ");
} catch (othermicrosoft) {
try {
request = new ActiveXObject( "Microsoft.XMLHTTP ");
} catch (failed) {
request = false;
}
}
}
if (!request)
alert( "加载ajax失败! ");
function richeng(){
var path= " <%=request.getContextPath()%> ";
var url = path+ "/getRicheng.do ";
request.open( "GET ", url, true);
request.send(null);
if (request.readyState == 4)//即 HTTP 就绪状态是 4
if (request.status == 200)//状态码是 200,它表示一切顺利
{
if(request.responseText!= " "){
alert(request.responseText);//服务器返回数据
}
}
else if (request.status == 404)
alert( "Request URL does not exist ");
else
alert( "Error: status code is " + request.status)
}
setInterval( "richeng() ",10000);
</script>
<body onload= "richeng(); ">
</body>
用意是每隔10秒就向服务器请求一次查询,就是getRicheng.do
getRichengActino如下:
AlarmManager mgr = new AlarmManager();
HttpSession sess = request.getSession(true);
Calendar c = Calendar.getInstance();
boolean flag=true;
String queue= " ";
final PrintWriter out=response.getWriter();
CalendarTaskDAOImpl ctdao=new CalendarTaskDAOImpl();
List lists=ctdao.getTasks(sess.getAttribute( "userID "));
System.out.println( "ajax ");
for(Iterator it=lists.iterator();it.hasNext();){
CyoaCalendarDetail taska = (CyoaCalendarDetail)it.next();
final String memo=taska.getMemo();
String importrace=taska.getImportace();
String mins=taska.getMins();
final String min=mins+importrace;
if(taska.getAwokeTime().after(new Date())){
if(queue!=null && !queue.equals( " ")){
String[] str1=queue.split( "; ");
for(int i=0;i <str1.length;i++){
if(str1[i].equals(taska.getCalendarDetailId())){
flag=false;
break;
}
}
}
if(flag){
queue=queue+String.valueOf(taska.getCalendarDetailId())+ "; ";
try{
c.setTime(taska.getAwokeTime());
mgr.addAlarm( "ComplexCron2 ",new int[]{c.get(Calendar.MINUTE)}, new int[]{c.get(Calendar.HOUR_OF_DAY)}, new int[]{c.get(Calendar.DATE)}, new int[]{c.get(Calendar.MONTH)}, new int[]{-1}, -1, new AlarmListener() {
public void handleAlarm(AlarmEntry entry) {
String str=min+ " ' "+memo+ " ' ";
// out.println(str);
System.out.println(str+ "----system ");
}
});
}catch(Exception e){e.printStackTrace();}
}
}
}
}
out.print(queue+ "--- ");
每次页面上都会弹了 alert(---);
可是 对于打印语句System.out.println( "ajax ");
却并不执行,而且也不能即时的向数据库取出数据, 每隔十秒的查询似乎根本没有进行,否则打印语句怎么会不执行呢,
第一次用ajax,本来觉得这个东西应该很神奇,很好用,没想到这么麻烦,这么不灵光.
知道的大哥,大姐们帮帮忙吧.
[解决办法]
request.send(null);
if (request.readyState == 4)//即 HTTP 就绪状态是 4
...
ajax不是这样用的...
既然ajax是异步,你应该用异步的思想去处理事件,
简单的:
request.onreadystatechange = function(){
if (request.readyState == 4 && request.status == 200) {
var result = request.responseText;//这个是返回的数据
...
}
具体代码写法可以搜索。
[解决办法]
是哦,我没有看到这一点。
[解决办法]
每次页面上都会弹了 alert(---);
说明应该是访问服务器了的吧,对于不打印那句话,我在另一个贴子里也看到了这样的问题,现在我的测试是这样的.
<%@taglib uri= "/WEB-INF/struts-html.tld " prefix= "html "%>
<%@page contentType= "text/html; charset=gb2312 "%>
<html:html>
<head>
<title> AJAX测试 </title>
<script type= "text/javascript ">
var xhr = false;
//var online=true;
//var count=0;
function getXHR(){
var x;
try {
x = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
x = new ActiveXObject( "Msxml2.XMLHTTP ");
} catch (othermicrosoft) {
try {
x = new ActiveXObject( "Microsoft.XMLHTTP ");
} catch (failed) {
x = false;
}
}
}
return x;
}
function init(){
window.setInterval( "requestData() ",3000);
}
var jj=1;
function requestData(){
try{
xhr=new ActiveXObject( "Msxml2.XMLHTTP ");
if(xhr){
//alert( "send data! ");
xhr.onreadyStatechange=processMessage;
xhr.open( "GET ", "messageProcessAction.do ");
jj++;
xhr.send(null);
}
}catch(e){
alert( "error3 ");
}
}
function processMessage(){
if(xhr.readyState==4){
if(xhr.status==200){
var message=xhr.responseText;
var msgArea=document.getElementById( "msgArea ");
msgArea.innerHTML=message+msgArea.innerHTML;
//message= " ";
}
}
}
</script>
</head>
<body >
<textarea cols= "20 " rows= "10 " id= "msgArea "> </textarea>
<%
System.out.println( "访问JSP了 ");
%>
<br />
<br />
<script>
init();
</script>
</body>
</html:html>
package com.awu.struts.action;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import java.io.*;
public class MessageProcessAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
System.out.println( "访问Action了 ");
int counter=1;
PrintWriter out=null;
try {
out = response.getWriter();
out.flush();
out.println( "fff ");
//out.println(Integer.parseInt(request.getParameter( "id "))+100+ " ");
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println( "有连接! ");
return null;
}
}