2016-06-17 14 views
5

Tôi cố gắng để gửi một cuộc gọi ajax để elasticsearch với khiên xác thựcElasticsearch: Shield Xác thực không làm việc trong AJAX gọi

$.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); 
       } 
}); 

nhưng tôi nhận được:

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

Tôi cũng đã cố gắng:

$.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); 
          } 
         }); 

nhưng tôi gặp lỗi 401 tương tự.

hôm sau tôi cố gắng:

$.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); 
            } 
           }); 

nhưng bây giờ tôi nhận được

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

cách thích hợp để gửi tên truy cập và mật khẩu để đàn hồi thông qua cuộc gọi ajax là gì?

đây là elasticsearch.yml tôi

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 

Trả lời

1

Cách giải quyết này là cấu hình CORS để chấp nhận các tiêu đề trong Authorization để elasticsearch.yml tập tin của bạn:

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

Ngoài ra hãy chắc chắn rằng bạn có ba cài đặt sau trong tệp elasticsearch.yml của bạn:

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

vì vậy với điều này, tôi có thể sử dụng chức năng gửi trước không? – AbtPst

+0

tôi vẫn nhận được 'XMLHttpRequest không thể tải http: // localhost: 9200/test2/test/_search. Trường header yêu cầu Cho phép Access-Control-Allow-Headers trong phản hồi preflight.' – AbtPst

+0

tôi đã thêm elasticsearch yml của mình lên trên – AbtPst

Các vấn đề liên quan