$.ajax({
url: 'jsondata.ashx',
type: 'GET',
dataType: 'json',
timeout: 1000,
cache: false,
beforeSend: LoadFunction, //加载执行方法
error: erryFunction, //错误执行方法
success: succFunction //成功执行方法
})
function LoadFunction() {
$("#list").html('加载中...')
}
function erryFunction() {
alert("error")
}
function succFunction(tt) {
$("#list").html('')
var json = eval(tt)//数组
$.each(json, function (index, item) {
//循环获取数据
var name = json[index].Name
var idnumber = json[index].IdNumber
var sex = json[index].Sex
$("#list").html($("#list").html() + "<br>" + name + " - " + idnumber + " - " + sex + "<br/>")
})
}
})
实现方法:定义一个form,action写servlet的地址即可跳转。参考代码如下:
<html>
<head>
<title>Test</title>
</head>
<body>
<form
action="/user/loginServlet.do"
method="post">
username:<input
name="username"
id="username"
type="text"/>
<br/>
password:<input
name="password"
id="password"
type="password"/>
<br/>
<input
type="submit"
name="btnLogin"
value="Login"/>
</form>
</body>
</html>
在servlet中的配置:
<web-app>
<!--
...
-->
<servlet>
<servlet-name>PageName</servlet-name>
<jsp-file>/success.html</jsp-file>
</servlet>
<!--
...
-->
<servlet-mapping>
<servlet-name>
PageName
</servlet-name>
<url-pattern>/user/*</url-pattern>
</servlet-mapping>
<!--
...
-->
</web-app>