标题:Ajax中xmlHttpRequest对象的status总是等于404
只看楼主
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
 问题点数:0 回复次数:19 
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
wyg4859
Rank: 2
等 级:新手上路
威 望:4
帖 子:883
专家分:0
注 册:2006-8-2
得分:0 
版主,把你的url输出来先,我帮你顶了先,能先解决我的问题吗?

http://www.找到组织!找到党!
2008-01-11 15:23
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
得分:0 
你那个我电脑上没法运行啊...

I'm here, as always...
2008-01-11 15:29
渚薰
Rank: 6Rank: 6
等 级:贵宾
威 望:22
帖 子:1132
专家分:0
注 册:2006-8-6
得分:0 
把完整的代码写出来,光给回调函数没啥可分析的……

个人ajax技术专题站: " target="_blank">http://www. 我不会闲你烦,只会闲你不够烦!
2008-01-14 09:01
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
渚薰
Rank: 6Rank: 6
等 级:贵宾
威 望:22
帖 子:1132
专家分:0
注 册:2006-8-6
得分:0 
明显servlet的问题……
另外,不要把XMLHttpRequest做为全局变量,好好去研究下Prototype是怎样封装xhr的吧

个人ajax技术专题站: " target="_blank">http://www. 我不会闲你烦,只会闲你不够烦!
2008-01-15 13:41
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
得分:0 
servlet有什么问题吗?厄,我这样写xmlHttp是为了方便,因为我机子上的ie支持这样的

I'm here, as always...
2008-01-15 14:08
渚薰
Rank: 6Rank: 6
等 级:贵宾
威 望:22
帖 子:1132
专家分:0
注 册:2006-8-6
得分:0 
这不是方不方便的问题
如果用全局变量来保存一个xhr的实例
那么每次使用的都是同一个实例。
举个简单的例子:
比如用户进行一次操作,你用这个xhr的实例去取数据,而当数据还没有返回时,用户又进行了一次操作,你又需要用同一个xhr的实例去取数据,是会出错的。
所以,每次进行异步调用,都必须是不同的xhr实例,而不能是同一个。
再另外,不知道你说的“用的方便”出于何利用,有现成的Prototype这么方便的框架摆在面前不用,却要自己写这么底层的代码……

个人ajax技术专题站: " target="_blank">http://www. 我不会闲你烦,只会闲你不够烦!
2008-01-15 17:03
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
得分:0 
因为在看《Ajax基础教程》,上面这么写的,以前没学过Ajax。还有,servlet有什么问题啊?哪里不对吗?

I'm here, as always...
2008-01-15 17:09



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-197039-1-1.html




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

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