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