关系到web的交互就离不开ajax。下面简要说一下项目中我用到的ajax方式。下面以get请求和post请求分别论述,若有不许确的地方万望海涵。web
1.get请求:ajax
特色:1.安全性较差,由于参数直接显示在url里面。例如,咱们向后台传输用户名,密码,相关信息直接显示在url(确定不能使用)。缓存
2.get请求有长度限制,不能超过最大长度。安全
3.get请求速度比较快。post
4 .因为get请求有缓存,通常请求静态资源用get请求。url
无参数的get请求:orm
$.ajax({
url: "/myweb/myinformation",
type: "get",
success: function (data) {
console.log(data);//请求成功之后返回数据打印出来
},
error: function () { //抛出错误
alert("出现异常");
}
});资源
有参数的get请求:get
$.ajax({
url: "myweb/myinformation?id=1&name=yanaly",
type: "get",
success: function (data) {
console.log(data);//请求成功之后返回数据打印出来
},
error: function () { //抛出错误
alert("出现异常");
}
});io
或
$.ajax({
url: "/myweb/myinformation",
type: "get",
data:{"name":"Yanaly","age":"18"},
success: function (data) {
console.log(data);//请求成功之后返回数据打印出来
},
error: function () {
alert("出现异常");
}
});
2.post请求
特色:
1.请求速度较慢。
2.安全性高。
3.请求传递参数没有限制
$.ajax({ url: "/myweb/myinformation", type: "post", data:{"name":"Yanaly"}, success: function (data) { console.log(data);//请求成功之后返回数据打印出来 }, error: function () { console.log(data);//请求成功之后返回数据打印出来 } });