Jquery authentication error

Consider this header information:

 Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
 Accept-Encoding:gzip, deflate, sdch
 Accept-Language:en-US,en;q=0.8
 Authorization:Basic TWluZVN0YXI6TWluZVN0YXI=
 Cache-Control:max-age=0
 Connection:keep-alive
 Host:*

      

This header information is for the URL. I am trying to get json value from this url. This url when one accessed it pops up for username and password.

TRY1:

      $.ajax({
        type:'GET',
      url: 'http://'+domainName+':9898/*/*',
      dataType:"json",
      username: '*',
      password: '*',
      async: false
      }).responseText;

      

TRY2

      $.ajax({
        type:'GET',
      url: 'http://'+domainName+':9898/jolokia/*/*',
      dataType:"json",
      crossDomain: true,
      beforeSend: function(xhr) { 
          xhr.setRequestHeader("Authorization", "Basic " + btoa('*' + ":" + '*')); 
          },
      async: false
      }).responseText;

      

TRY3:

Tried adding this to web.xml tomcat.

<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
 <param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
</init-param>
<init-param>
 <param-name>cors.allowed.headers</param-name>
 <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-    Method,Access-Control-Request-Headers</param-value>
 </init-param>
 <init-param>
 <param-name>cors.exposed.headers</param-name>
 <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-    value>
 </init-param>
 <init-param>
   <param-name>cors.support.credentials</param-name>
 <param-value>true</param-value>
 </init-param>
 <init-param>
   <param-name>cors.preflight.maxage</param-name>
   <param-value>10</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

      

But still I can't access the url, but it gives the following error:

     GET http://***.**.**.**:9898/*/* 401 (Unauthorized)
    jquery.min.js:5 XMLHttpRequest cannot load http://**.**.**.**:9898/*/*. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin http://localhost:8080 is therefore not allowed access. The response had HTTP status code 401.(index):1 Uncaught SyntaxError: Unexpected token u

      

I just need to access the url and get the json values ​​from that url. The url is protected with a username and password that I know.

I don't understand how this could be a cross domain issue. When I remove authentication from the url, this code works fine:

      $.ajax({
        type:'GET',
      url: 'http://'+domainName+':9898/*/*',
      dataType:"json",
      async: false
      }).responseText;

      

But as soon as I authenticate the url the code crashes, after applying the above steps the code doesn't work.

+3


source to share


3 answers


Try to set additional jquery ajax parameter:

xhrFields: { withCredentials: true }



This will enable the functionality of cors.

0


source


try removing dataType: "json" from ajax method. Maybe server methods get json string but don't use json content-type



0


source


There is no need for jsonp. I got it using this:

    $.ajax({
    xhrFields: { withCredentials: true },
    type:'GET',
     url: 'http://'+domainName+':9898/jolokia/*/*',
     dataType:"json",
     crossDomain: true,
      async: false
      }).responseText;

      

0


source







All Articles