cs83863896 2012-02-28 07:05
浏览 354
已采纳

初学AJAX 总是404

小弟初学AJAX,遇到404问题,baidu,google都不能解决。
附上源码,哪位大哥给点意见,多谢多谢。

web.xml
[code="xml"]

RefreshModelList
ajax.RefreshModelListServlet


RefreshModelList
/RefreshModelList

[/code]
RefreshModelListServlet.java
[code="java"]
package ajax;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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

public class RefreshModelListServlet extends HttpServlet{

/**
 * 
 */
private static final long serialVersionUID = 1L;

private static List availableModels = new ArrayList();

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
    response.setContentType("text/html; charset=UTF-8");
    int modelYear = Integer.parseInt(request.getParameter("modelYear"));
    String make = request.getParameter("make");

    StringBuffer results = new StringBuffer("<models>");
    MakeModelYear availableModel = null;

    for(Iterator it = availableModels.iterator(); it.hasNext();){
        availableModel = (MakeModelYear)it.next();
        if(availableModel.modelYear == modelYear){
            if(availableModel.make.equals(make)){
                results.append("<model>");
                results.append("availableModel.model");
                results.append("</model>");
            }

        }
    }
    results.append("</models>");
    response.setContentType("text/xml");
    response.getWriter().write(results.toString());

}



@Override
public void init() throws ServletException {

    availableModels.add(new MakeModelYear(2006, "Dodge", "Charger"));
    availableModels.add(new MakeModelYear(2006, "Dodge", "Magnum"));
    availableModels.add(new MakeModelYear(2006, "Dodge", "Ram"));
    availableModels.add(new MakeModelYear(2006, "Dodge", "Viper"));
    availableModels.add(new MakeModelYear(1995, "Dodge", "Avenger"));
    availableModels.add(new MakeModelYear(1995, "Dodge", "Intrepid"));
    availableModels.add(new MakeModelYear(1995, "Dodge", "Neon"));
    availableModels.add(new MakeModelYear(1995, "Dodge", "Spirit"));
    availableModels.add(new MakeModelYear(1985, "Dodge", "Aries"));
    availableModels.add(new MakeModelYear(1985, "Dodge", "Daytona"));
    availableModels.add(new MakeModelYear(1985, "Dodge", "Omni"));
    availableModels.add(new MakeModelYear(1970, "Dodge", "Challenger"));
    availableModels.add(new MakeModelYear(1970, "Dodge", "Charger"));
    availableModels.add(new MakeModelYear(1970, "Dodge", "Coronet"));
    availableModels.add(new MakeModelYear(1970, "Dodge", "Dart"));

    availableModels.add(new MakeModelYear(2006, "Chevrolet", "Colorado"));
    availableModels.add(new MakeModelYear(2006, "Chevrolet", "Corvette"));
    availableModels.add(new MakeModelYear(2006, "Chevrolet", "Equinox"));
    availableModels.add(new MakeModelYear(2006, "Chevrolet", "Monte Carlo"));
    availableModels.add(new MakeModelYear(1995, "Chevrolet", "Beretta"));
    availableModels.add(new MakeModelYear(1995, "Chevrolet", "Camaro"));
    availableModels.add(new MakeModelYear(1995, "Chevrolet", "Cavalier"));
    availableModels.add(new MakeModelYear(1995, "Chevrolet", "Lumina"));
    availableModels.add(new MakeModelYear(1985, "Chevrolet", "Cavalier"));
    availableModels.add(new MakeModelYear(1985, "Chevrolet", "Chevette"));
    availableModels.add(new MakeModelYear(1985, "Chevrolet", "Celebrity"));
    availableModels.add(new MakeModelYear(1985, "Chevrolet", "Citation II"));
    availableModels.add(new MakeModelYear(1970, "Chevrolet", "Bel Air"));
    availableModels.add(new MakeModelYear(1970, "Chevrolet", "Caprice"));
    availableModels.add(new MakeModelYear(1970, "Chevrolet", "Chevelle"));
    availableModels.add(new MakeModelYear(1970, "Chevrolet", "Monte Carlo"));

    availableModels.add(new MakeModelYear(2006, "Pontiac", "G6"));
    availableModels.add(new MakeModelYear(2006, "Pontiac", "Grand Prix"));
    availableModels.add(new MakeModelYear(2006, "Pontiac", "Solstice"));
    availableModels.add(new MakeModelYear(2006, "Pontiac", "Vibe"));
    availableModels.add(new MakeModelYear(1995, "Pontiac", "Bonneville"));
    availableModels.add(new MakeModelYear(1995, "Pontiac", "Grand Am"));
    availableModels.add(new MakeModelYear(1995, "Pontiac", "Grand Prix"));
    availableModels.add(new MakeModelYear(1995, "Pontiac", "Firebird"));
    availableModels.add(new MakeModelYear(1985, "Pontiac", "6000"));
    availableModels.add(new MakeModelYear(1985, "Pontiac", "Fiero"));
    availableModels.add(new MakeModelYear(1985, "Pontiac", "Grand Prix"));
    availableModels.add(new MakeModelYear(1985, "Pontiac", "Parisienne"));
    availableModels.add(new MakeModelYear(1970, "Pontiac", "Catalina"));
    availableModels.add(new MakeModelYear(1970, "Pontiac", "GTO"));
    availableModels.add(new MakeModelYear(1970, "Pontiac", "LeMans"));
    availableModels.add(new MakeModelYear(1970, "Pontiac", "Tempest"));
}



@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    processRequest(req, resp);
}


private static class MakeModelYear{

    private int modelYear;
    private String make;
    private String model;

    public MakeModelYear (int modelYear,String make,String model){
    this.modelYear = modelYear;
    this.make = make;
    this.model = model;
}
}

}
[/code]

dynamic.html
[code="html"]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">



Dynamically Filling Lists var xmlHttp; function createXMLHttpRequest() { if (window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } } function refreshModelList() { var make = document.getElementById("make").value; var modelYear = document.getElementById("modelYear").value; if (make == "" || modelYear == "") { clearModelList(); return; } var url = "/RefreshModelList?" + createQueryString(make, modelYear) + "&ts=" + new Date().getTime(); [color=red]alert(url);[/color]//这里有值 createXMLHttpRequest(); xmlHttp.onreadystatechange = handleStateChange; xmlHttp.open("GET", url, false); xmlHttp.send(null); } function createQueryString(make, modelYear) { var queryString = "make=" + make + "&modelYear=" + modelYear; return queryString; } function handleStateChange() { if (xmlHttp.readyState == 4) { [color=red]alert(xmlHttp.status);[/color] //这里总是404 if (xmlHttp.status == 200) { updateModelsList(); } } } function updateModelsList() { clearModelsList(); var models = document.getElementById("models"); var results = xmlHttp.responseXML.getElementsByTagName("model"); var option = null; for ( var i = 0; i < results.length; i++) { option = document.createElement("option"); option.appendChild(document .createTextNode(result[i].firstChild.nodeValue)); models.appendChild(option); } } function clearModelList() { var models = document.getElementById("models"); while (models.childNodes.length > 0) { models.removeChild(models.childNode[0]); } }



Select Model Year and Make



Model Year: id="modelYear" onchange="refreshModelList();">
Select One
2006
1995
1985
1970


Make: id="make" onchange="refreshModelList();">
Select One
Chevrolet
Doge
Pontiac
    </select> <br> <br> <span style="font-weight: bold;">Models: </span>
    <br> <select id="models" size="6" style="width: 300px;"></select>
</form>



[/code]

展开全部

  • 写回答

2条回答 默认 最新

  • ll89308839 2012-02-28 08:04
    关注

    明显路径错了嘛
    url ="<%=request.getContextPath()%>/RefreshModelList?" + createQueryString(make, modelYear)+ "&ts=" + new Date().getTime();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥30 matlab ode45 未发现警告,但是运行出错
  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部