2017-03-14 1 views
0

Je dois créer la même requête dans AJAX.Ajax avec authentification

Quelqu'un peut-il me conseiller comment inclure l'authentification?

i essayé user/pass comme paramètres GET et URL de changement

https://user:[email protected]/token

i utilise vue des ressources, mais peut également utiliser jQuery.ajax ou 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); 

Répondre

0

essayer d'utiliser jQuery de beforeSend callback pour ajouter un en-tête HTTP avec votre chargement d'authentification:

beforeSend: function (xhr) { 
    xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password)); 
},