2016-06-17 4 views
5

ich mit Schild AuthentifizierungElasticsearch: Schild-Authentifizierung nicht in AJAX-Aufruf arbeitet

$.ajax({ 
     url: 'http://localhost/test2/test/_search', 
     type: 'POST', 
     //contentType: 'application/json; charset=UTF-8', 
     crossDomain: true, 
     dataType: 'json', 
     username: "admin", 
     password: "admin123", 
     data: JSON.stringify(queryBody), 
     success: function(response) { 
       alert(response) 
       var data = response.hits.hits; 
       var titleArray = []; 
       //alert(data.length); 
       if (data.length > 0) { 
       /* 
        if (data.length > 5) 
         data.length=5; 
       */ 
        for (var i = 0; i < data.length; i++) {    
         if(data[i].fields.Title[0].indexOf(settings.fieldValue) > -1) 
             { 
              titleArray.push(data[i].fields.DocumentID[0]+":"+data[i].fields.Title[0]); 
             } 
            } 
       responseS(titleArray); 
       titleArray=[]; 
       } else { } 
     }, 
     error: function(jqXHR, textStatus, errorThrown) { 
         var jso = jQuery.parseJSON(jqXHR.responseText); 
         alert('section', 'error', '(' + jqXHR.status + ') ' + errorThrown + ' --<br />' + jso.error); 
       } 
}); 

Elasticsearch versuchen einen Ajax-Aufruf zu senden, aber ich erhalte:

POST http://localhost:9200/test2/test/_search 401(Unauthorized)

ich auch versucht:

$.ajax({ 
          url: 'http://admin:[email protected]/test2/test/_search', 
          type: 'POST', 
          //contentType: 'application/json; charset=UTF-8', 
          crossDomain: true, 
          dataType: 'json', 
          data: JSON.stringify(queryBody), 
          success: function(response) { 
           alert(response) 
           var data = response.hits.hits; 

           var titleArray = []; 

           //alert(data.length); 
           if (data.length > 0) { 
            /* 
            if (data.length > 5) 
             data.length=5; 
            */ 
            for (var i = 0; i < data.length; i++) { 

             if(data[i].fields.Title[0].indexOf(settings.fieldValue) > -1) 
             { 

              titleArray.push(data[i].fields.DocumentID[0]+":"+data[i].fields.Title[0]); 
             } 
            } 

            responseS(titleArray); 
            titleArray=[]; 

           } else { 

           } 


          }, 

          error: function(jqXHR, textStatus, errorThrown) { 
           var jso = jQuery.parseJSON(jqXHR.responseText); 
           alert('section', 'error', '(' + jqXHR.status + ') ' + errorThrown + ' --<br />' + jso.error); 
          } 
         }); 

aber ich habe den gleichen Fehler 401.

nächstes habe ich versucht:

$.ajax({ 
            url: 'http://localhost/test2/test/_search', 
            type: 'POST', 
            //contentType: 'application/json; charset=UTF-8', 
            crossDomain: true, 
            dataType: 'json', 
            data: JSON.stringify(queryBody), 
            beforeSend: function (xhr) { 
    xhr.setRequestHeader ("Authorization", "Basic " + btoa("admin:admin123")); 
},         success: function(response) { 
             alert(response) 
             var data = response.hits.hits; 

             var titleArray = []; 

             //alert(data.length); 
             if (data.length > 0) { 
              /* 
              if (data.length > 5) 
               data.length=5; 
              */ 
              for (var i = 0; i < data.length; i++) { 

               if(data[i].fields.Title[0].indexOf(settings.fieldValue) > -1) 
               { 

                titleArray.push(data[i].fields.DocumentID[0]+":"+data[i].fields.Title[0]); 
               } 
              } 

              responseS(titleArray); 
              titleArray=[]; 

             } else { 

             } 


            }, 

            error: function(jqXHR, textStatus, errorThrown) { 
             var jso = jQuery.parseJSON(jqXHR.responseText); 
             alert('section', 'error', '(' + jqXHR.status + ') ' + errorThrown + ' --<br />' + jso.error); 
            } 
           }); 

aber ich habe jetzt

XMLHttpRequest cannot load http://localhost:9200/test2/test/_search . Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response. localhost/:1 Uncaught SyntaxError: Unexpected token u

Was ist der richtige Weg ist, Benutzernamen und Passwort elastisch über Ajax-Aufruf zu senden?

hier ist mein elasticsearch.yml

action.disable_delete_all_indices: true 

http.cors.enabled: true 
http.cors.allow-origin: "*" 
http.cors.allow-headers: "Authorization, X-Requested-With, Content-Type, Content-Length" 
http.cors.allow-credentials: true 

bootstrap.mlockall: true 

# For reference: https://www.elastic.co/guide/en/elasticsearch/guide/current/_limiting_memory_usage.html 

# controls how much heap space is allocated to fielddata. When you run a query that requires access to new field values, 
# it will load the values into memory and then try to add them to fielddata. If the resulting fielddata size would 
# exceed the specified size, other values would be evicted in order to make space. 
indices.fielddata.cache.size: 40% 

# The fielddata circuit breaker limits the size of fielddata to 60% of the heap, by default. 
indices.breaker.fielddata.limit: 60% 

# The request circuit breaker estimates the size of structures required to complete other parts of a request, 
# such as creating aggregation buckets, and limits them to 40% of the heap, by default. 
indices.breaker.request.limit: 40% 

# The total circuit breaker wraps the request and fielddata circuit breakers to ensure that the combination 
# of the two doesn’t use more than 70% of the heap by default. 
indices.breaker.total.limit: 70% 
#shield.enabled: false 
shield: 
    authc: 
    realms: 
     native1: 
     type: native 
     order: 0 
    realms: 
     esusers: 
     type: esusers 
     order: 1 
     files: 
      users: ElasticSearch\elasticsearch-2.3.1\elasticsearch-2.3.1\config\shield\users 
      users_roles: ElasticSearch\elasticsearch-2.3.1\elasticsearch-2.3.1\config\shield\users_role 

Antwort

1

Die Art und Weise, dies zu lösen ist CORS zu konfigurieren, dass die Authorization Header zu akzeptieren, um Ihre elasticsearch.yml Datei:

http.cors.allow-headers: "Authorization, X-Requested-With, Content-Type, Content-Length" 

auch sicherstellen, dass Sie haben Die folgenden drei Einstellungen in Ihrer Datei:

http.cors.enabled: true 
http.cors.allow-origin: "/https?:\/\/localhost(:[0-9]+)?/" 
http.cors.allow-credentials: true 
+0

also mit dieser, kann ich die vor senden senden funktion? – AbtPst

+0

Ich bekomme immer noch 'XMLHttpRequest kann nicht laden http: // localhost: 9200/test2/test/_search. Anforderungs-Header-Feld Autorisierung ist nicht erlaubt von Access-Control-Erlaube-Header in Preflight-Antwort. – AbtPst

+0

Ich habe meine Elasticsearch Yml über – AbtPst