Reputation: 3105
I have a couch server running and I am attempting to access it from a web application served from port 80. I am having difficulty authenticating.
The code I am using to test is:
var host = 'http://localhost:5984'
var url = host + "/wells/"
var username = 'will'
var password = 'secret'
// Succeeds
$.post(
host + "/_session",
{ name: username, password: password },
function() {
console.log( 'cookie', document.cookie ) // doesn't include AuthSession
$.get( url ) // Fails 401 unauthorized
}
)
var hostParts = host.split( '://' )
var hostWithAuth = hostParts[1] + "://" + username + ":" + password + "@" + hostParts[2]
$.get( hostWithAuth ) // Fails unauthorized, credentials removed from url
// Fails unauthorized
$.ajax( {
url: url,
username: username,
password: password,
success: function() {
console.log( arguments )
},
xhrFields: { withCredentials: true },
headers: {
Authorization: "Basic " + btoa( username + ":" + password )
}
} )
// Fails unauthorized
var xmlHttp = new XMLHttpRequest()
xmlHttp.open( 'GET', url, false, username, password )
xmlHttp.send( null )
console.log( xmlHttp.responseText )
It seems as though my credentials are being removed when I try to pass them in the url and the cookie isn't sent to the server when using sessions.
Upvotes: 0
Views: 399
Reputation: 4679
Your requests fails because of Same-Origin Policy since you made AJAX request to url with different port (not 80). You need to setup CORS correctly to let your requests succeeded.
P.S. And why not to use jquery.couch.js as client library instead of own $.ajax calls? It handles a lot of useful bits and provides nicer API.
Upvotes: 1