AJAX 请求密码
AJAX Request with Password
我有以下PHP/CURL。我需要通过 JS/AJAX (Axios)
发送
如何适配JS?
$username = 'test';
$password = 'test';
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, 'https://example.com/pc_api/index.php/token');
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_handle, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt($curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
要与 axios 一起使用,请使用 auth
进行基本身份验证:
axios({
method: 'get',
url: 'https://example.com/pc_api/index.php/token',
responseType: 'json', // default is json
auth: {
username: 'test',
password: 'test'
}
}).then(function(response) {
console.log(response);
}).catch(function(error) {
console.log(error);
});
我有以下PHP/CURL。我需要通过 JS/AJAX (Axios)
发送如何适配JS?
$username = 'test';
$password = 'test';
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, 'https://example.com/pc_api/index.php/token');
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_handle, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt($curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
要与 axios 一起使用,请使用 auth
进行基本身份验证:
axios({
method: 'get',
url: 'https://example.com/pc_api/index.php/token',
responseType: 'json', // default is json
auth: {
username: 'test',
password: 'test'
}
}).then(function(response) {
console.log(response);
}).catch(function(error) {
console.log(error);
});