erknrio
3/27/2018 - 4:08 PM

Ajax authentication

Ajax authentication

// Simulando llamada POST a una API local, con GET igual
$.ajax({
  "url": "http://127.0.0.1/api/v1/access/",
  "method": "POST",
  "data": {"foo": "bar"},
  "beforeSend": function (xhr) {
    xhr.withCredentials = true
    // btoa codifica a base64
    xhr.setRequestHeader ("Authorization", "Basic " + btoa("user:password"));
  }
}).done(function (response) {
    alert("OK");
    window.console.log(response);
}).fail(function (jqXHR, textStatus, errorThrown) {
    alert("KO :-(");
    window.console.error(textStatus + ': ' + errorThrown);
});