网站首页 > 文章中心 > 其它

ajax接收服务端返回数据

作者:小编 更新时间:2023-10-08 13:50:15 浏览量:394人看过

接收ajax传过来的数据

function checkUser(ouser){

var uname=ouser.value;

ajax接收服务端返回数据-图1

if(!uname){

alert("用户名不能为空");

ouser.focus;

}

//发送请求到服务器,判断用户名是否存在

//Ajax代码实现

// 发送请求到服务器,判断用户名是否存在

// 请求字符串

/ar url = "servlet/doReg?uname="+uname; //GET 方式

ajax接收服务端返回数据-图2

var url = "servlet/doReg"; //POST 方式

var userinfo="uname="+uname;

// 1. 创建XMLHttpRequest组件

xmlHttpRequest = createXmlHttpRequest();

xmlHttpRequest.onreadystatechange = haoLeJiaoWo;

//xmlHttpRequest.open("GET",url,true); //GET方式

xmlHttpRequest.open("POST",url,true);//POST方式

xmlHttpRequest.send(userinfo);

function haoLeJiaoWo(){

var b = xmlHttpRequest.responseText;

if(b=="true"){

alert("用户名已经存在");

}else{

alert("用户名可以使用");

function createXmlHttpRequest(){

return new XMLHttpRequest();

return new ActiveXObject("Microsoft.XMLHTTP");

服务器端代码:

ajax接收服务端返回数据-图3

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

PrintWriter out = response.getWriter();

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

System.out.println(uname);

boolean uExists=false;

if(uname.equals("hdjr"))

{uExists=true;

out.print(uExists);

out.flush();

out.close();

/**

* The doPost method of the servlet.

*

* This method is called when a form has its tag value method equals to post.

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

doGet(request,response);

前后端 前端:用户可以看见的界面,我们一般浏览网页时看到的界面,展现网页数据给用户看的地方. 后端:要将前端的数据存储到数据库中,并且把数据库的数据传递给前端. 数据库:存储数据的地方. 前后端交互:前端将数据传递给服务端,并且能够从服务端接收到返回的数据的过程.

回调函数设置一个data参数,用来接收返回值

$.ajax({

cache: false, //是否缓存

url: "?"+stype, //路径

success:function(data){ //成功后返回的回调方法

var json// data就是后台返回的数据

})

以上就是土嘎嘎小编大虾米为大家整理的相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章