I have the following PHP/CURL. I need to send it via JS/AJAX (Axios)
How to adapt to 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);
To use with axios, Use auth
for basic authentication :
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);
});
Check request config params