1.首先我们基于curl的请求参数示例:

curl http://localhost:8080/oauth/token -X POST -u client:fucksecurity -d "grant_type=refresh_token&refresh_token=1a1fb46e-8ab4-4a3b-84c4-e70892eaa570"
其中的 -u 表示的是身份认证的用户名和密码。后来尝试过Jquery的$.ajax的username和password去传这个两个对象,发现请求一直未通过。后来查了一下ajax的api。


 上面的截图是api的,发现这两个属性,必须要用于XML HttpRequest对象。以下是模拟上面请求功能的demo.

var url = "http://localhost:8080/oauth/token?grant_type=password&username=shawn&password=fucksecurity";
//1/得到xhr对象
var xhr = XHR();
//2.注册状态变化监听器
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
token = JSON.parse(xhr.responseText);
}else{
//4.向服务器发出请求
xhr.send("userName=client");
}
}
}
//3.建立与服务器的连接
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//4.向服务器发出请求
xhr.send("userName="+name);