gpt4 book ai didi

javascript - 难以向 CouchDB 进行身份验证

转载 作者:行者123 更新时间:2023-12-03 12:37:17 25 4
gpt4 key购买 nike

我有一个正在运行的沙发服务器,我正在尝试从端口 80 提供服务的 Web 应用程序访问它。我在身份验证方面遇到困难。

我用来测试的代码是:

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 中传递凭据时,似乎我的凭据被删除,并且在使用 session 时 cookie 不会发送到服务器。

最佳答案

由于 Same-Origin Policy,您的请求失败因为您使用不同的端口(不是 80)向 url 发出了 AJAX 请求。您需要设置CORS正确地让您的请求成功。

附注为什么不使用 jquery.couch.js作为客户端库而不是自己的 $.ajax 调用?它处理许多有用的位并提供更好的 API。

关于javascript - 难以向 CouchDB 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23682143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com