【发布时间】:2015-10-23 05:05:14
【问题描述】:
我一直在研究将 AJAX POST 请求发送到我的 API 的方法,并且我正在尝试了解如何正确传递基本身份验证凭据。
Interface API
https://www.example.com/app/ --------> https://api.example.com/
使用我在 StackOverflow 上找到的这个例子——任何人都无法查看 JS 的源代码,无法以明文形式查看我的用户名和密码,并可以访问我的所有 API 函数吗?
如果是这样,我如何传递我的用户名和密码而不向全世界展示?
$.ajax({
url: 'yoururl',
username : username,
password :password,
type: 'POST',
contentType: 'application/x-www-form-urlencoded',
dataType: "text",
xhrFields:
{
withCredentials: true
},
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + ":" + password));
}
});
【问题讨论】:
标签: ajax authentication basic-authentication