$({//这里是json格式的数据}).ajax方法
(1).这个方法是全局函数.
(2).这个方法括号中的只能是json的数据格式.
(3).查阅这个函数的文档...
案例:
get.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>get()方法</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../js/jquery-1.4.2.js"></script>
<style type="text/css">
div,span{
width: 140px;
height: 140px;
margin: 20px;
background: #9999CC;
border: #000 1px solid;
float:left;
font-size: 17px;
font-family:Roman;
}
div.mini{
width: 30px;
height: 30px;
background: #CC66FF;
border: #000 1px solid;
font-size: 12px;
font-family:Roman;
}
div.visible{
display:none;
}
</style>
<!--引入jquery的js库-->
</head>
<body>
<form action="" name="form1" id="form1">
<input type="text" name="username" id="username" value="zhang"><br>
<input type="text" name="psw" id="psw" value="99999"><br>
<input type="button" id="b1" value="登陆">
</form>
<div id="one">
</div>
</body>
<script language="JavaScript">
$(function(){
$("#b1").click(function(){
var json = {
username : $("#username").val(),
psw : $("#psw").val()
}
$().ajax({
url:"get.jsp",//请求的页面
type:"get",//请求方式为get
async:true,//是否异步
data:json,//请求的参数
dataType:"json",//服务器返回的数据类型
success:function(data,textStatus){//请求成功后的函数
var json=eval("("+data+")");
alert(json.province);
},
});
});
});
</script>
</html>
get.jsp页面<%@ page language="java" pageEncoding="UTF-8"%>
<%
System.out.println(request.getMethod());
System.out.println("connection server success!");
System.out.println("username = "+request.getParameter("username"));
System.out.println("password = "+request.getParameter("psw"));
//响应JSON格式:jQuery提供的get()或post()方法时,数据格式为JSON格式时,只能使用第三方工具构建JSON格式的数据内容,不能使用手工方式
out.println("[{'province':'jilinsheng'},{'province':'liaoningsheng'},{'province':'shandongsheng'}]");
%>
火狐浏览器:控制台: