jquery – 使用Ajax调用的http基本身份验证

前端之家收集整理的这篇文章主要介绍了jquery – 使用Ajax调用的http基本身份验证前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要联系服务器以使用HTTP服务.

尝试使用https://example.com/service通过浏览器访问服务我获得了基本的身份验证对话框.

将URL更改为https:// username:password@example.com/service可以轻松绕过该URL.

尝试使用Ajax执行相同操作始终会导致401 Unauthorized:

  1. $.ajax({
  2. url: 'https://username:password@example.com/service',// rest repeats
  3. type: "GET",async: true,success: function(text) { alert('success'); },error: function (text) { alert('error') },complete: function(text) { alert('complete'); }
  4. });
  1. $.ajax({
  2. url: 'https://example.com/service',username: username,password: password,// snip repeat
  3. });
  1. $.ajax({
  2. url: 'https://example.com/service',beforeSend: function(xhr) {
  3. xhr.setRequestHeader("Authorization","Basic "
  4. + btoa(username + ":" + password));
  5. },// snip repeat
  6. });

解决方法

我自己也在努力解决类似的问题..
诀窍是使用jsonp(呃):
  1. $.ajax({
  2. url: "https://localhost:8443/v1/accounts",type: 'GET',dataType: 'jsonp',beforeSend: function (xhr) {
  3. xhr.setRequestHeader('Authorization','Basic bHVpZ2lAZ21haWwuY29tOmFiYzEyMzQ1');
  4. }
  5. })

猜你在找的jQuery相关文章