麻烦看下 ,我哪里错了,没显示出来?

来源:2-3 发送请求及处理响应

兮兮666

2019-09-12 22:28:34

package com.imooc.ajax;


import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


/**

 * Servlet implementation class ContentServlet

 */

@WebServlet("/content")

public class ContentServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public ContentServlet() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.getWriter().print("<b>I'm server content</b>");

}


}



<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

<input id="btnLoad" type="button" value="加载">

<div id="divContent"></div>

<script type="text/javascript">

document.getElementById("btnLoad").onclick=function(){

//1.创建XmlHttpRequest对象

var xmlhttp;

if(window.XMLHttpRequest){

xmlhttp=new XMLHttpRequest();

}else{

xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")

}

//console.log(xmlhttp);

//2.发送Ajax

xmlhttp.open("GET","/ajax/content",true);

xmlhttp.send();

//3.处理服务器响应

xmlhttp.onreadystatechange=function(){

if(xmlhttp.readState==4 && xmlhttp.status==200){

var tt=xmlhttp.responseText;

document.getElementById("divContent").innerHTML=tt;

}

}

}

</script>

</body>

</html>


写回答

1回答

好帮手慕阿满

2019-09-14

同学你好,在处理服务器响应的if判断中,readyStatus的单词拼写错误,少了y,如:

http://img.mukewang.com/climg/5d7c588c093c3c0704850153.jpg

建议同学修改一下再试试。

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

0

0 学习 · 9666 问题

查看课程