getJSON方式请求服务器
register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'register.jsp' starting page</title>
<script type="text/javascript" src="<%=basePath%>js/jquery-1.12.4.js"></script>
<script type="text/javascript">
$(document).ready(
function() { $("#loginName").blur(
function() {
var name = this.value;
alert(name);
if (name == "") {
$("#check").text("用户名不能为空");
} else {//使用ajax发送异步请求,判断用户名是否存在
$.getJSON(
"http://localhost:8080/ajaxstu1/check",
"name="+name, callBack);
}
function callBack(checkTag) {
if (checkTag == true) {
$("#check").text("用户名已使用");
} else if (checkTag ==false) {
$("#check").text("用户名可以使用");
}
} }//end blur function );
});
</script> </head>
<!--
/* $.ajax({
"url" : "check",
"type" : "get",
"data" : "name=" + name,
"dataType" : "text",
"success" : callBack,
"error" : function() {
alert("系统正在更新,稍后再试");
}
}); */
-->
<body>
<form action="">
<table>
<tr>
<td>昵称:</td>
<td><input type="text" id="loginName" name="name" />
</td>
<td><span id="check"></span>
</td>
</tr>
</table>
</form> </body>
</html>
注意:使用 $.getJSON( "http://localhost:8080/ajaxstu1/check", "name="+name, callBack); 去请求服务器时,服务器返回的是json对象,在callBack回调函数中做
checkTag == true 判断时,就不能使用checkTag =="true"的方式了,否则,结果出不来!!!
CheckUserServlet.java
package cn.bdqn.xsh.controller; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.alibaba.fastjson.JSON; public class CheckUserServlet extends HttpServlet{ private static final long serialVersionUID = 8418279663598505788L; @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException { // resp.setCharacterEncoding("UTF-8");
String name = req.getParameter("name");
System.out.println(name);
boolean tag = true;
if(name.equals("liuch")){
tag=true;
}else{
tag=false;
}
PrintWriter out = resp.getWriter(); // 使用$.getJSON方式进行异步请求
String returnStr=JSON.toJSONString(tag); //把tag转成JSON格式
System.out.println("returnStr is:"+returnStr);
out.print(returnStr);
out.flush();
out.close(); } @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");//
this.doGet(req, resp);
} }
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <servlet>
<servlet-name>CheckUserServlet</servlet-name>
<servlet-class>cn.bdqn.xsh.controller.CheckUserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CheckUserServlet</servlet-name>
<url-pattern>/check</url-pattern>
</servlet-mapping> </web-app>
使用$.get()方式请求服务器和使用$.getJSON()方式请求服务器,返回值的类型不同,前者是一个字符串,后者不是,看下图浏览器调试界面的区别:
$.get()方式请求服务器:

使用$.getJSON()方式请求服务器:

此外,可以在callBack回调函数中加一句:console.log(typeof checkTag ); 就能知道服务器返回的值的类型了
function callBack(checkTag) {
console.log(typeof result);
31 if (checkTag == true) {
32 $("#check").text("用户名已使用");
33 } else if (checkTag ==false) {
34 $("#check").text("用户名可以使用");
35 }
36 }
getJSON方式请求服务器的更多相关文章
- NSURLRequest POST方式请求服务器示例
http://lizhuang.iteye.com/blog/1833297 1. 准备阶段 NSString *urlString = [NSString stringWithFormat:@&q ...
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
讲这个之前,我们先来说说get和post两种请求的区别吧!!! 1. GET提交的数据会放在URL之后,以?分割URL和传输数据,参数之间以&相连,如EditPosts.jsp?name=te ...
- Android 使用HTTP(get和post)方式登陆服务器
package com.wuyou.submittoserver; import android.os.Bundle; import android.support.v7.app.ActionBarA ...
- Android请求服务器的两种方式--post, get的区别
android中用get和post方式向服务器提交请求_疯狂之桥_新浪博客http://blog.sina.com.cn/s/blog_a46817ff01017yxt.html Android提交数 ...
- IOS 请求服务器的方式
IOS 中请求服务器的方式主要有Get 和Post . Get :[1]向服务器发索取数据的一种请求; [2]获取信息,而不是修改信息,类似数据库查询功能一样,数据不会被修改;请求的参数会跟在url后 ...
- android中用get和post方式向服务器提交请求
通过get和post方式向服务器发送请求首先说一下get和post的区别get请求方式是将提交的参数拼接在url地址后面,例如http://www.baidu.com/index.jsp?num=23 ...
- HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
1.转自:https://blog.csdn.net/alinshen/article/details/78221567?utm_source=blogxgwz4 /* * 演示通过HttpClie ...
- android 之HttpURLConnection的post,get方式请求数据
get方式和post方式的区别: 1.请求的URL地址不同: post:"http://xx:8081//servlet/LoginServlet" get:http://xxx: ...
- Ajax-(get/post/jQuery方式请求)
< !DOCTYPE html > < html xmlns = "http://www.w3.org/1999/xhtml" > < head &g ...
随机推荐
- 2017华南理工华为杯D bx回文
比赛的时候队友过了,补补题XD. 题目链接:https://scut.online/p/125(赛后补题) 125. 笔芯回文 题目描述 bx有一个长度一个字符串S,bx可以对其进行若干次操作 ...
- AtCoder ABC 140D Face Produces Unhappiness
题目链接:https://atcoder.jp/contests/abc140/tasks/abc140_d 题目大意 有一对 N 个人, 用字符串 S 表示, S[i] 如果等于 'L' 说明这个人 ...
- Mac007--Mysq服务端&客户端安装
一.安装Mysql服务端与Navicat Premium客户端 参见博客:https://blog.csdn.net/wtdask/article/details/79025674 安装mysql服务 ...
- git报错-Initial commit Untracked files nothing added to commit but untracked ……
文章转自 https://www.jianshu.com/p/61c3db30d488 在目标执行命令 git stratus 报错 根据上面的文章,可以解决问题.不行的话,请留言. 感谢你的阅读
- 提交代码到github
1. 下载git 点击download下载即可.下载地址:https://gitforwindows.org/ 2. 注册github github地址:https://github.com/ 一定要 ...
- luogu P3919 [模板]可持久化数组(可持久化线段树/平衡树)(主席树)
luogu P3919 [模板]可持久化数组(可持久化线段树/平衡树) 题目 #include<iostream> #include<cstdlib> #include< ...
- ZOJ 2706 Thermal Death of the Universe
Thermal Death of the Universe Time Limit: 10 Seconds Memory Limi ...
- 复制书稿 (dp+贪心)
[题目描述] 现在要把m本有顺序的书分给k个人复制(抄写),每一个人的抄写速度都一样,一本书不允许给两个(或以上)的人抄写,分给每一个人的书,必须是连续的,比如不能把第一.第三和第四本书给同一个人抄写 ...
- Spring Data Redis实战之提供RedisTemplate
参考: http://www.cnblogs.com/edwinchen/p/3816938.html 本项目创建的是Maven项目 一.pom.xml引入dependencies <depen ...
- ToolStripComboBox 绑定数据
//添加ComboBox tcbbQueryCondition.ComboBox.DataSource = RelationalOperators.GetAllOperators(); tcbbQue ...