Ajax 有身份验证
Ajax with Authentication
我需要在 AJAX 中创建相同的请求。
有人可以告诉我如何包括身份验证吗?
我尝试将 user/pass 作为 GET 参数并将 URL 更改为
https://user:pwd@www.example.com/token
我使用 vue-resource 但也可以使用 jQuery.ajax 或 axios
$username = 'user';
$password = 'pwd';
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, 'https://www.example.com/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);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
尝试使用 jQuery 的 beforeSend 回调将 HTTP header 添加到您的身份验证负载:
beforeSend: function (xhr) {
xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password));
},
我需要在 AJAX 中创建相同的请求。
有人可以告诉我如何包括身份验证吗?
我尝试将 user/pass 作为 GET 参数并将 URL 更改为
https://user:pwd@www.example.com/token
我使用 vue-resource 但也可以使用 jQuery.ajax 或 axios
$username = 'user';
$password = 'pwd';
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, 'https://www.example.com/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);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
尝试使用 jQuery 的 beforeSend 回调将 HTTP header 添加到您的身份验证负载:
beforeSend: function (xhr) {
xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password));
},