ajax的url
ajax的URL与web.xml文件的URL是什么关系,还有这个例子是不是不用后台代码,总的来说不是很懂ajax的URL
ajax的URL与web.xml文件的URL是什么关系,还有这个例子是不是不用后台代码,总的来说不是很懂ajax的URL
2016-10-13
<script type="text/javascript">
function ajax(){
var xmlHttp=new XMLHttpRequest();//建立请求对象
var phone=document.getElementById("phone") ;//获得请求参数
var order=document.getElementById("order");
var url = "LoginServlet";//建立url连接
var data=phone.value;
xmlHttp.open("post",url,true);//打开服务器连接
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send("phone="+data);//发送请求
xmlHttp.onreadystatechange = function() {//服务器响应
if (xmlHttp.readyState == 4) {
var response = xmlHttp.responseText;
document.getElementById("order").innerHTML = response;
}
}
}
</script>
<script>
//要发送post请求,send发送请求那里我试了好像不行,监听状态的方法我是这么写的你看看吧
</script>
能帮我看下吗,我的servlet获取不到值,但前台值已发送,谢谢啦
//servlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String phone=request.getParameter("phone");//这块获取不到值,
//request.setAttribute("phone", phone);
System.out.println("adsdadad:"+phone);
}
//ajax
function ajax(){
var xmlHttp=new XMLHttpRequest();//建立请求对象
var phone=document.getElementById("phone") ;//获得请求参数
var order=document.getElementById("order");
var url = "loginServlet";//建立url连接
var data=phone.value;
xmlHttp.open("get",url,true);//打开服务器连接
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.onreadystatechange = updatePage;
xmlHttp.send(data);//发送请求
function updatePage() {//服务器响应
if (xmlHttp.readyState == 4) {
var response = xmlHttp.responseText;
document.getElementById("order").innerHTML = response;
}
}
}
</script>
//web.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>loginSer</servlet-name>
<servlet-class>sevlet.loginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginSer</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>ajax.html</welcome-file>
</welcome-file-list>
举报