猿问

Ajax连接servlet连接问题 ajax无法访问servlet?

前端HTML

<body>

    <button id="getvalue">

    请点击

    </button>

    <div id = "value"></div>  </body>

Ajax代码

$("#getvalue").click(function(){

$.ajax({

url:"/servlet/doajax",

cache:true,

type:"GET",

data:{username:"admin"},

async:false,

datatype:"html",

error:function(request){

alert("cuowu");

},

success:function(data){

$("#value").html(data);

}

});

});

servlet代码

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {


response.setContentType("text/html");

String username = request.getParameter("username");

System.out.println(username);

PrintWriter out = response.getWriter();

out.print("welcome "+username);

out.flush();

out.close();

}

xml配置

 <servlet>

    <description>This is the description of my J2EE component</description>

    <display-name>This is the display name of my J2EE component</display-name>

    <servlet-name>doajax</servlet-name>

    <servlet-class>servlet.doajax</servlet-class>

  </servlet>


  <servlet-mapping>

    <servlet-name>doajax</servlet-name>

    <url-pattern>/servlet/doajax</url-pattern>

  </servlet-mapping>


黑色嘉年华ALEX
浏览 2761回答 1
1回答

__YXQ__

ajax请求里的url应该是doajax
随时随地看视频慕课网APP

相关分类

Java
JQuery
我要回答