[英]Javascript (JQuery/CouchDB) function doesn't work but only when called from button onClick
In my HTML head I first include jquery.min.js, jquery.couch.js and twitter-bootstrap js (in that order) and then I have a function called logmein() as follows: 在我的HTML头中,我首先包含jquery.min.js,jquery.couch.js和twitter-bootstrap js(按此顺序),然后有一个名为logmein()的函数,如下所示:
<script>
$.couch.urlPrefix = "http://127.0.0.1:5984"
function logmein() {$.couch.login({
name: "testuser",
password: "test",
success: function() { alert("Success!") },
error: function() { alert("Login failed") }
});
}
</script>
With username and password hard-coded for testing. 使用用户名和密码进行硬编码以进行测试。
I then have a button: 然后,我有一个按钮:
<button id="submit" class="btn btn-primary" onClick="logmein()">Sign in</button>
When I try and click submit, I get this error: 当我尝试单击提交时,出现此错误:
jquery.couch.js:216 TypeError: 'null' is not an object (evaluating 'resp.error')
However, if I type logmein() directly into Safari's web developer console, it works just fine! 但是,如果我直接在Safari的Web开发人员控制台中键入logmein(),它就可以正常工作! What am I doing wrong here? 我在这里做错了什么?
Edit: I think it maybe related to this: https://stackoverflow.com/a/6792725/637562 though I don't quite understand why (to my knowledge, I was running it on the same protocol/host). 编辑:我认为它可能与此有关: https : //stackoverflow.com/a/6792725/637562,尽管我不太清楚为什么(据我所知,我在同一协议/主机上运行它)。
You can try changing your logmein function to run on the click event of #submit: 您可以尝试更改登录功能以在#submit的click事件上运行:
$('#submit').click(function(){
$.couch.urlPrefix = "http://127.0.0.1:5984";
$.couch.login({
name: "testuser",
password: "test",
success: function() { alert("Success!") },
error: function() { alert("Login failed") }
});
});
Also, it seems that the urlPrefix should be inside the logmein() function 另外,似乎urlPrefix应该位于logmein()函数内部
function logmein()
{
$.couch.urlPrefix = "http://127.0.0.1:5984";
$.couch.login({
name: "testuser",
password: "test",
success: function() { alert("Success!") },
error: function() { alert("Login failed") }
});
}
"Solved" “解决了”
If anyone else is having this issue, I'm still not exactly sure what caused the problem. 如果还有其他人遇到此问题,我仍然不确定是什么原因引起的。 Google Chrome gave a more detailed error message, suggesting the same origin policy had been violated, even though I don't believe this to be the case. Google Chrome浏览器给出了更详细的错误消息,表明违反了同一原产地政策,即使我认为情况并非如此。
Despite this, there's a much simpler solution all without Javascript: the CouchDB Session API. 尽管如此,没有Java脚本,还有一个更简单的解决方案:CouchDB Session API。
<form action="http://127.0.0.1:5984/_session/" method="post">
<input type="text" name="name">
<input type="password" name="password">
<button id="submit">Sign in</button>
Where 127.0.0.1 is a local couchdb instance, and should be replaced with your couchdb server address.... 其中127.0.0.1是本地ouchdb实例,应替换为您的ouchdb服务器地址。
More details here: http://wiki.apache.org/couchdb/Session_API including how to set up automatic page redirection on form submission. 此处有更多详细信息: http : //wiki.apache.org/couchdb/Session_API,包括如何在表单提交时设置自动页面重定向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.