难以验证到CouchDB [英] Difficulty authenticating to CouchDB

查看:37
本文介绍了难以验证到CouchDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在运行的长沙发服务器,并且尝试从端口80提供的Web应用程序访问它.我无法进行身份验证.

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.

我要测试的代码是:

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 )

当我尝试在url中传递凭据时,似乎我的凭据已被删除,并且在使用会话时cookie没有发送到服务器.

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.

推荐答案

由于 CORS 才能让您的请求成功.

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.以及为什么不使用 jquery.couch.js 作为客户端库,而不是自己的$ .ajax调用?它处理了许多有用的内容,并提供了更好的API.

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.

这篇关于难以验证到CouchDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆