语法:
$.ajax(参数);
参数是json格式
{
type: “GET或POST”,
url : “请求的服务器地址”,
data:“仅限于POST提交数据k1=v1&k2=v2…kn=vn”,
success:function(responseText){做事}
}
<%@ page language="java" pageEncoding="UTF-8" %>
<%
String path = request.getContextPath ( );
String basePath = request.getScheme ( ) + "://" + request.getServerName ( ) + ":" + request.getServerPort ( ) + path + "/";
%>
改造成POST提交
(1)服务端
package com.njwbhz.mood.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet ( "/checkUserName.do" )
public class CheckUserNameServlet extends HttpServlet {
@Override
protected void doPost ( HttpServletRequest request , HttpServletResponse response ) throws ServletException, IOException {
this.doGet ( request , response );
}
@Override
protected void doGet ( HttpServletRequest request , HttpServletResponse response ) throws ServletException, IOException {
String result = "";//相应的结果数据
String userName = request.getParameter ( "userName" );
//模拟 假设数据库中存在用户名“sa”
if ( userName.equals ( "sa" ) ) {
//不可用
result = "0";
} else {
//可用
result = "1";
}
//把result数据响应回去
//发送到(写到)客户端浏览器
//流
PrintWriter out = response.getWriter ( );
out.write ( result );
out.flush ( );
out.close ( );
}
}
(2)客户端
<%@ page language="java" pageEncoding="UTF-8" %>
<%
String path = request.getContextPath ( );
String basePath = request.getScheme ( ) + "://" + request.getServerName ( ) + ":" + request.getServerPort ( ) + path + "/";
%>



