Skip to content

Instantly share code, notes, and snippets.

@torufurukawa
Created May 18, 2012 05:45
Show Gist options
  • Save torufurukawa/2723412 to your computer and use it in GitHub Desktop.
Save torufurukawa/2723412 to your computer and use it in GitHub Desktop.
jQuery で Basic 認証のかかったリソースにアクセスする
<html>
<head>
<title>client</title>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript" src="jquery.base64.js"></script>
<script type="text/javascript">
function getinfo() {
$.ajax({
url: "http://example.com/",
success: function(data){alert('OK');},
error: function(jqXHR, textStatus, errorThrown){
alert(textStatus+": "+errorThrown);
},
beforeSend: function(xhr) {
var credentials = $.base64.encode("username:password");
xhr.setRequestHeader("Authorization", "Basic " + credentials);
},
});
}
</script>
</head>
<body>
<button onclick="getinfo();">Press Me!</button>
<div id="result"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment