AJAX api.js恳求浏览插口API必须客户名登陆密码登陆

阅读  ·  发布日期 2021-03-26 01:12  ·  admin

加了真实身份验证的API插口详细地址,浏览API的情况下,会弹出来对话必须键入客户名和登陆密码开展受权,键入恰当后便可以获得回到的JSON数据信息

image.png

用postman浏览插口时效性果以下:

image.png

 

那麼怎样根据AJAX完成,恳求api详细地址,客户名和登陆密码是怎样传入后台管理去验证的?

在$.ajax里边设定headers主要参数

headers: {
  Authorization :  Basic   + b64EncodeUnicode(USERNAME +  :  + PASSWORD)
 },

比如:

api.ajax({
url :url,
method :  post ,
cache : false,
headers: {
  Authorization :  Basic   + b64EncodeUnicode(USERNAME +  :  + PASSWORD)
 },
data : {
values: {
grant_type: password ,
username:  serviceuser1 ,
password: admin_2 
}, function(ret, err) {
log(ret);
});
function b64EncodeUnicode(str) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
return String.fromCharCode( 0x  + p1);
}

。假如要传回到的token,必须在headers里设定

headers: {
  Authorization :  Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ZpY2V1c2VyMSIsIm5hbWUiOiLmnI3liqHkurrlkZgiLCJlbWFpbCI6IiIsInBob25lIjoiIiwidHlwZSI6IjMiLCJhdmF0YXIiOiJodHRwczovL2d3LmFsaXBheW9iamVjdHMuY29tL3pvcy9ybXNwb3J0YWwvQmlhemZhbnhtYW1OUm94eFZ4a2EucG5nIiwib3JnSWQiOjAsInRlbmFudElkIjowLCJhZG1pbiI6ZmFsc2UsInB3ZEludFNldCI6ZmFsc2V9LCJzY29wZSI6WyJyZWFkIl0sImV4cCI5MTU4NTcxMjg3MywianRpIjoiMTdlY2FlYWYtODM2OS00Mjc3LWIyODAtYWU0N2Y1ZDdlOTM4IiwiY2xpZW50X2lkIjoidGVzdF9jbGllbnQifQ.8cIR3AZBaLlCpHW571keuhZlEckDeeew6m9H7r_qp5g , token_type : bearer , refresh_token : eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ZpY2V1c2VyMSIsIm5hbWUiOiLmnI3liqHkurrlkZgiLCJlbWFpbCI6IiIsInBob25lIjoiIiwidHlwZSI6IjMiLCJhdmF0YXIiOiJodHRwczovL2d3LmFsaXBheW9iamVjdHMuY29tL3pvcy9ybXNwb3J0YWwvQmlhemZhbnhtYW1OUm94eFZ4a2EucG5nIiwib3JnSWQiOjAsInRlbmFudElkIjowLCJhZG1pbiI6ZmFsc2UsInB3ZEludFNldCI6ZmFsc2V9LCJzY29wZSI6WyJyZWFkIl0sImF0aSI6IjE3ZWNhZWFmLTgzNjktNDI3Ny1iMjgwLWFlNDdmNWQ3ZTkzOCIsImV4cCI5MTU4NTgxMzY3MywianRpIjoiYTJlNmMwYjAtNDMwMi00MjMyLThiMTQtMmExYjJmOTBmZDMxIiwiY2xpZW50X2lkIjoidGVzdF9jbGllbnQifQ.HsIr5Ljqn-YGf13GxeREQyalaPYHJXo8h2GBkBFznCwg 
 },

postman恳求截屏:

image.png

文中由网站站长原創或搜集,不意味着本网站观点,倘若转截,请标明出處: