Ajax中向服务器传递参数


Ajax使用的是xmlhttp请求,向服务器发送请求传递参数也在xmlhttm请求中完成。

首先创造一个xmlhttp请求,使用函数

?View Code JAVASCRIPT
function createXMLHttpRequest(){
 
var xmlHttp;
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
 
return xmlHttp;
}

在调用函数中传递参数:

?View Code JAVASCRIPT
function show(){
//var url=querystring();
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET","show?name=hello",true);
xmlHttp.send(null);
}

意思是向服务器请求show这个servlet,传递的参数为name=hello。

服务器端的servlet接收这个参数并且返回:

public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException
{
 
String getname = req.getParameter("name");
 
PrintWriter out = res.getWriter();
out.print(getname);
 
}

程序的前后部分省略。

在客户端接收服务器的响应:

老杨个人网站,作者老杨原文地址,转载请注明出处。

如果喜欢本站,敬请订阅:老杨个人网站。谢谢惠顾!


分类: 信息技术
标签: , ,

我要抢沙发

:emotion58 :emotion57 :emotion563 :emotion562 :emotion561 :emotion560 :emotion56 :emotion559 :emotion558 :emotion557 :emotion556 :emotion554 :emotion552 :emotion551 :emotion550 :emotion55 :emotion547 :emotion546 :emotion544 :emotion543 :emotion542 :emotion541 :emotion540 :emotion54 :emotion539 :emotion538 :emotion537 :emotion536 :emotion535 :emotion534 :emotion533 :emotion532 :emotion531 :emotion530 :emotion53 :emotion529 :emotion526 :emotion525 :emotion524 :emotion523 :emotion522 :emotion521 :emotion520 :emotion52 :emotion519 :emotion518 :emotion517 :emotion516 :emotion515 :emotion514 :emotion513 :emotion512 :emotion511 :emotion510 :emotion51 :emotion119 :emotion113