文章目录
GET
$("#password").blur(function () {
$.ajax({
type: "GET",
url: "http://localhost:8080/userService/checkIfUserExistById/1",
contentType: 'application/json;charset=utf-8', //设置请求头信息
//data: "devId=" + devId, //传给服务端的数据
dataType: "json", //希望服务端返回的数据类型
success: function (res) {
alert("success");
alert(res);
},
error: function (result) {
alert("fail");
}
});
});
POST
$("#username").blur(function () {
$.ajax({
url:"http://localhost:8080/userService/modifyPassword",
type: "POST",
data: {"id": "123", "newPassword": "123"},
contentType: "application/x-www-form-urlencoded",
dataType: "json", //希望服务端返回的数据类型
success: function(data) {
alert("success");
alert(data); // Object
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
},
complete: function(XMLHttpRequest, textStatus) {
/*请求完成后回调函数 (请求success 和 error之后均调用)。
这个回调函数得到2个参数:XMLHTTPRequest) 对象和
一个描述请求状态的字符串("success", "notmodified", "nocontent","error", "timeout", "abort", 或者 "parsererror") 。
*/
}
});
});
解决跨域访问
在接口上添加@CrossOrigin
$.ajax contenType是appliation/json的时候,spring mvc后台无法接受data参数
https://blog.csdn.net/G1248019684/article/details/50850386?spm=1001.2014.3001.5502
Spring @CrossOrigin 注解原理
https://blog.csdn.net/weixin_33691817/article/details/92392464