我需要联系服务器以使用HTTP服务.
尝试使用https://example.com/service通过浏览器访问服务我获得了基本的身份验证对话框.
将URL更改为https:// username:password@example.com/service可以轻松绕过该URL.
尝试使用Ajax执行相同操作始终会导致401 Unauthorized:
$.ajax({ url: 'https://username:password@example.com/service',// rest repeats type: "GET",async: true,success: function(text) { alert('success'); },error: function (text) { alert('error') },complete: function(text) { alert('complete'); } });
$.ajax({ url: 'https://example.com/service',username: username,password: password,// snip repeat });
$.ajax({ url: 'https://example.com/service',beforeSend: function(xhr) { xhr.setRequestHeader("Authorization","Basic " + btoa(username + ":" + password)); },// snip repeat });
解决方法
我自己也在努力解决类似的问题..
诀窍是使用jsonp(呃):
诀窍是使用jsonp(呃):
$.ajax({ url: "https://localhost:8443/v1/accounts",type: 'GET',dataType: 'jsonp',beforeSend: function (xhr) { xhr.setRequestHeader('Authorization','Basic bHVpZ2lAZ21haWwuY29tOmFiYzEyMzQ1'); } })