I'm doing a CORS attempt trying a autocomplete functionality, just as a test, from my local server to other one which is online. This server has a basic authentication configured in the folder I'm trying to access. So I've done the ajax function (I'm using JQuery UI autocomplete) and I have configured the php script to return me a JSON. Everything was working fine from the same server.
This is what I've done:
$(function() {
      $( "#field" ).autocomplete({
        minLength: 1,
        source: function (request, response) {
          console.log(request.term);
          $.ajax({
            type: 'POST',
            url: url,
            data: {
              'idautor': 417,
              'lat': 43.56,
              'lon': -5.96,
              'etiqueta': request.term
            },
            crossDomain: true,
            dataType: 'json',
            beforeSend: function (xhr) {
              xhr.setRequestHeader ("Authorization", btoa(username + ":" + password));
            },
            success: function( data ) {
              response( $.map( data["pines"], function( item ) {
                return {
                  value: item["nombre"],
                  label: item["nombre"],
                  idsitio: item["idsitio"],
                  autor: item["autor"]
                }
              }));
            }
          }).then(function(data) {
            console.log('data', data);
          }, 
          function(err) {
            console.log('err', err);
          });
        },
        select: function (event, ui) {
          alert("El sitio con id "+ui.item.idsitio+" y autor "+ui.item.autor+" ha sido pulsado.");
        }
      })
    });  
The problem is when I try to use it from my local server, basically the cross-origin HTTP request I'm talking about I get the following error in my browser console:
XMLHttpRequest cannot load URL Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 401
So I've researched the subject and I found couple of promissing and easy solutions to my problem. I've tried to add the headers to the php file:
header("Access-Control-Allow-Origin: *"); 
header("Access-Control-Allow-Credentials: true"); 
header("Access-Control-Allow-Methods: GET,POST,OPTIONS");
header("Access-Control-Allow-Headers: accept, authorization, content-type");  
Also tried to add them to my .htaccess file with no success at all. I'm still getting the same error. Any clue about what am I missing?  
Just in case and for further information this is my XHR connection record when I send the request, where I cannot see the new headers I've added manually in the php.

 
     
    