| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 10671 人关注过本帖
标题:Ajax中xmlHttpRequest对象的status总是等于404
取消只看楼主 加入收藏
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
收藏
 问题点数:0 回复次数:9 
Ajax中xmlHttpRequest对象的status总是等于404
我按照《Ajax基础教程》上的toolTip例子写的,其中一段:
function callback() {
        //alert("callback1");
        if (xmlHttp.readyState == 4) {
            //alert("callback");
            if (xmlHttp.status == 200) {
                alert("callback2");
                setData(xmlHttp.responseXML);
            }
        }
    }
这里,status==200不行,我改成404就可以了,说明没有找到资源。可是写的servlet也没有问题啊,为什么会找不到啊?
var url = "ToolTipServlet?key=" + escape(element.id);
这个是xmlHttp.open("GET", url, true)里面的url,我检查过了,servlet名字大小写都是对的,是escape函数有问题吗?
搜索更多相关主题的帖子: Ajax status quot 对象 callback 
2008-01-11 15:17
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
收藏
得分:0 
你那个我电脑上没法运行啊...

I'm here, as always...
2008-01-11 15:29
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
收藏
得分:0 
厄,奇怪的问题,今天早上一来再运行就好了,没有任何的改动...
为什么会这样?

I'm here, as always...
2008-01-14 09:45
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
收藏
得分:0 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Ajax Tool Tip Example</title>
<script>
    var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    var dataDiv;
    var dataTable;
    var dataTableBody;
    var offsetEl;
    
    function initVars() {
        //alert("initVars");
        dataTableBody = document.getElementById("courseDataBody");
        dataTable = document.getElementById("courseData");
        dataDiv = document.getElementById("popup");
    }
    
    function getCourseData(element) {
        //alert("getCourseData");
        initVars();
        offsetEl = element;
        var url = "ToolTipServlet?key=" + escape(element.id);
        xmlHttp.open("GET", url, true);
        xmlHttp.onreadystatechange = callback;
        xmlHttp.send(null);
    }
    
    function callback() {
        //alert("callback1");
        if (xmlHttp.readyState == 4) {
            //alert("callback");
            if (xmlHttp.status == 200) {
                alert("callback2");
                setData(xmlHttp.responseXML);
            }
        }
    }
    
    function setData(courseData) {
        //alert("setData");
        clearData();
        setOffsets();
        var length = courseData.getElementsByTagName("length")[0].firstChild.data;
        var par = courseData.getElementsByTagName("par")[0].firstChild.data;
        var row, row2;
        var parData = "Par:" + par;
        var lengthData = "Length:" + length;
        row = createRow(parData);
        row2 = createRow(lengthData);
        dataTableBody.appendChild(row);
        dataTableBody.appendChild(row2);
    }
    
    function createRow(data) {
        var row, cell, txtNode;
        row = document.createElement("tr");
        cell = document.createElement("td");
        cell.setAttribute("bgcolor", "#FFFAFA");
        cell.setAttribute("border", "0");
        txtNode = document.createTextNode(data);
        cell.appendChild(txtNode);
        row.appendChild(cell);
        return row;
    }
    
    function setOffsets() {
        var end = offsetEl.offsetWidth;
        var top = calculateOffsetTop(offsetEl);
        dataDiv.style.border = "black 1px solid";
        dataDiv.style.left = end + 15 + "px";
        dataDiv.style.top = top + "px";
    }
    
    function calculateOffsetTop(field) {
        return calculateOffset(field, "offsetTop");
    }
    
    function calculateOffset(field, attr) {
        var offset = 0;
        while(field) {
            offset += field[attr];
            field = field.offsetParent;
        }
        return offset;
    }
    
    function clearData() {
        var ind = dataTableBody.childNodes.length;
        for(var i = ind - 1; i >= 0; i--) {
            dataTableBody.removeChild(dataTableBody.childNodes[i]);
        }
        dataDiv.style.border = "none";
    }

</script>
</head>
<body>
<h1>Ajax Tool Tip Example</h1><br/>
<h3>Golf Course</h3><br/>
<table id="courses" bgcolor="#FFFAFA" border="1" cellspacing="0" cellpadding="2" >
    <tbody>
        <tr>
            <td id="1" onmouseover="getCourseData(this);" onmouseout="clearData();" >August National</td>
        </tr>
        <tr>
            <td id="2" onmouseover="getCourseData(this);" onmouseout="clearData();" >Pinehurst No.2</td>
        </tr>
        <tr>
            <td id="3" onmouseover="getCourseData(this);" onmouseout="clearData();" >St. Andrews Links</td>
        </tr>
        <tr>
            <td id="4" onmouseover="getCourseData(this);" onmouseout="clearData();" >Golf Club</td>
        </tr>
    </tbody>
</table>
<div style="position:absolute;" id="popup">
    <table id="courseData" bgcolor="#FFFAFA" border="1" cellspacing="0" cellpadding="2">
        <tbody id="courseDataBody"></tbody>
    </table>
</div>
</body>
</html>
然后是servlet:
package src;

import
import
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class for Servlet: ToolTipServlet
 *
 */
 public class ToolTipServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#HttpServlet()
     */
    
    private Map courses = new HashMap();
    
    public void init(ServletConfig config) throws ServletException {
        CourseData a = new CourseData(72, 7290);
        CourseData b = new CourseData(70, 7214);
        CourseData c = new CourseData(72, 6566);
        CourseData d = new CourseData(70, 7392);
        courses.put(new Integer(1), a);
        courses.put(new Integer(2), b);
        courses.put(new Integer(3), c);
        courses.put(new Integer(4), d);
    }
    public ToolTipServlet() {
        super();
    }       
    
    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        Integer key = Integer.valueOf(request.getParameter("key"));
        CourseData data = (CourseData)courses.get(key);
        response.setContentType("text/xml");
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();
        out.println("<response>");
        out.println("<par>" + data.getPar() + "</par>");
        out.println("<length>" + data.getLength() + "</length>");
        out.println("</response>");
        out.close();
    }      
    
    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }   
    
    private class CourseData {
        private int par;
        private int length;
        
        public CourseData(int par, int length) {
            this.par = par;
            this.length = length;
        }

        public int getLength() {
            return length;
        }

        public int getPar() {
            return par;
        }
    }
}
红色那行上周怎么样都输不出来,今天一来就好了...

I'm here, as always...
2008-01-14 09:48
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
收藏
得分:0 
servlet有什么问题吗?厄,我这样写xmlHttp是为了方便,因为我机子上的ie支持这样的

I'm here, as always...
2008-01-15 14:08
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
收藏
得分:0 
因为在看《Ajax基础教程》,上面这么写的,以前没学过Ajax。还有,servlet有什么问题啊?哪里不对吗?

I'm here, as always...
2008-01-15 17:09
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
收藏
得分:0 
又来上班了,还是没想通servlet有什么问题,能告诉我吗?

I'm here, as always...
2008-01-16 09:41
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
收藏
得分:0 
有可能,因为之前的tomcat还有其他项目在运行,第二天来再运行就可以了,不过应该跟servlet没关系吧,书上的servlet就这么写的

I'm here, as always...
2008-01-16 12:37
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
收藏
得分:0 
你昨天不是说我servlet有问题吗?没说servlet容器有问题啊

I'm here, as always...
2008-01-16 15:30
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
收藏
得分:0 

To黄袖标:只找到一个在线的,我现在就在看:
[url]http://www.1cn.biz/docs/LanguageTech/JavaServerFaces/JavaServerFaces.htm[/url]

I'm here, as always...
2008-01-16 17:35
快速回复:Ajax中xmlHttpRequest对象的status总是等于404
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019458 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved