jQuery 基本身份验证失败

jquery basic authentication failing

本文关键字:失败 身份验证 jQuery      更新时间:2023-09-26

请考虑以下标头信息:

 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:*

此标头信息适用于 URL。我正在尝试从此网址获取 json 值。单独访问此 URL 时会弹出用户名和密码。

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:

尝试将其添加到雄猫的网络.xml中。

<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>

但是我仍然无法访问该网址,它仍然给出以下错误:

     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

我只需要访问 url 并从该 url 获取 json 值。该URL受用户名和密码保护,我知道。

我现在不知道这怎么可能是一个跨域问题。当我从 url 中删除身份验证时,此代码工作正常:

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

但是一旦我验证了 url,代码就会失败,在应用上述步骤后,代码也会失败。

尝试设置额外的 jquery ajax 参数:

xhrFields: { withCredentials: true }

这将启用 cors 功能。

尝试从ajax方法中删除dataType:"json"。也许服务器方法获取 json 字符串但不使用 json 内容类型

不需要 jsonp。我用这个得到它:

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