jQuery 基本身份验证失败
jquery basic authentication failing
请考虑以下标头信息:
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;
相关文章:
- 添加@javascript标记时,Cucumber中的HTTP身份验证失败
- postmates api的AJAX中的HTTP身份验证失败
- Spring Security中的基本身份验证(身份验证失败消息)
- 将变量从jQuery传递到WebMethod(身份验证失败)
- Django 身份验证:CSRF 失败
- jQuery 基本身份验证失败
- MongoError: 身份验证失败.- 连接到MongoLab时出现问题
- 对passportjs+Node.js+Express.js Rest API调用成功身份验证和失败的路由处理程序
- Gapi 的身份验证弹出窗口立即关闭,并失败并显示 401 错误
- 通过令牌进行身份验证,如果令牌失败,则使用Passport通过会话进行身份验证
- Sails.JS-NTLM身份验证失败时重试
- Phonegap + Sencha + Android: ajax请求与基本访问身份验证失败
- 对firebase的多个自定义身份验证请求失败
- fetch()在Firefox中使用SSL客户端身份验证失败
- NodeJS Passport身份验证-始终重定向到失败url
- Express.js Passport身份验证自动失败跳过策略
- Firebase电子邮件身份验证网络请求失败
- $http拦截器:身份验证失败时重定向
- ruby on rails-请求javascript规范失败,设计身份验证
- 使用node、passport和react进行Github身份验证失败