简体   繁体   English

Javascript(JQuery / CouchDB)函数不起作用,仅当从按钮onClick调用时才起作用

[英]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.

相关问题 将按钮<a onclick>发送到另一个页面时</a> ,只有按钮onclick起作用, <a onclick>标记在javascript中不起作用</a> - Only button onclick works, <a onclick> tag doesn't work in javascript when sending parameter to another page 用于显示饼图的onclick()jQuery / JavaScript按钮不起作用 - onclick() jQuery/JavaScript Button to display piechart doesn't work 从onclick调用3个不同的javascript函数不起作用,仅执行第一个函数 - Calling 3 different javascript functions from onclick doesn't work, executes only the first function JavaScript按钮onclick不起作用 - Javascript button onclick doesn't work onclick/Button 在 Javascript/HTML 中不起作用 - onclick/Button Doesn't work in Javascript/HTML 使用addEventListener或onclick调用时,该功能不起作用,未显示任何错误消息 - Function doesn't work when called with addEventListener or onclick, doesn't show up any error msg jQuery 附加 onclick 功能不起作用 - jQuery append onclick function doesn't work 使用locatin.href =…的Javascript函数从控制台调用时有效,而不是从按钮中的onClick调用时有效 - Javascript function using `locatin.href = …` works when called from console, not when called from `onClick` in a button 具有重定向功能的JavaScript onclick函数不起作用 - JavaScript onclick function with redirect doesn't work javascript onclick函数不适用于参数 - javascript onclick function doesn't work with parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM