ajax请求以body传输书写方式
$.ajax({
type: "post",
contentType:"application/json",
dataType: 'json',
data:JSON.stringify({"type":"1"}),
url: "http://www.demo.com/test/test",
success: function(data) {
console.log(data)
}
});
如上代码,最重要的一步是
contentType:"application/json",
否则传输数据后台解析json数据会出错
后台接收方式@RequestBody(required = false) String param
ajax请求Authorization Basic
$.ajax({
type: "POST",
dataType: 'json',
url: gloableUrl+"/api/v1/oauth2/token",
data: {grant_type: 'client_credentials'},
beforeSend: function(request) {
var key ='username:password';
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
request.setRequestHeader("grant_type", 'client_credentials');
request.setRequestHeader ("Authorization", "Basic " + btoa(key));
},
success: function(data) {
console.log(data)
}
})
})