I'm trying do a ajax request and I get Error 401. The username and password is correct, if I write the url in the browser and put the username and password works fine.

All comments with xhr.setRequestHeader are test and returns error 401.

  $.ajax({
type: "POST",
url: url,
data: {'format':'JSON','caller':206,'minDur':8,'maxDur':60},
success: function(data){
  console.log(data)
},
xhrFields: {
    withCredentials: true
},
beforeSend: function (xhr) {
  // xhr.setRequestHeader ("WWW-Authenticate", "Basic " + btoa("****:****"));
  // xhr.setRequestHeader ("WWW-Authenticate", "Basic " + "****:****");
  // xhr.setRequestHeader ("Authorization", "Basic " + "****:****");
  // xhr.setRequestHeader("WWW-Authenticate", 'Basic ' + btoa('****:****'));
  // xhr.setRequestHeader("Access-Control-Allow-Origin", '*');
  // xhr.setRequestHeader("Access-Control-Allow-Origin", 'http://jordidev:5557');
},
crossDomain: true,
username: '****',
password: '****',
dataType: 'json',


error: function (xhr, ajaxOptions, thrownError) {

  // If the connection fails, log the error and return an empty set.
  console.log(xhr);
  console.log(xhr.statusText);
  console.log(xhr.statusCode);
  console.log(xhr.responseText);
  console.log(ajaxOptions);
  console.log(thrownError);

}

0 Answers