JQuery在Sinatra上的GET请求总是导致错误

3

我正在使用Sinatra来托管一个简单的服务。以下是我的整个应用程序:

require 'sinatra'

get '/hello' do
  'Hello world!'
end

然后我在./public目录下有一个单独的html文件,尝试对我的服务进行GET请求:

<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("a").click(function(event) {
                $.get("/hello", function() { alert("first success"); })
                        .success(function() { alert("second success"); })
                        .error(function(jqXHR) { alert("error: " + jqXHR); });
            })
        });
    </script>
</head>
<body>
<a href="">Link</a>
</body>
</html>

当我在HTML页面上点击链接时,我在服务器日志中看到以下内容:
``` 0:0:0:0:0:0:0:1%0 - - [16/Feb/2011 10:37:42] "GET /hello HTTP/1.1" 200 12 0.0030 localhost - - [16/Feb/2011:10:37:42 CST] "GET /hello HTTP/1.1" 200 12 Referer -> /hello ```
这看起来很好,但无论我怎样调整,我只能让错误回调被调用,从未成功过。
我的两个问题是: 1)我做错了什么? 2)我该如何调试此类问题?传递给错误回调的对象只是一个Object,并且我无法获取有关错误的信息。
谢谢!
2个回答

2
在我的测试中,我发现链接的点击代码没有 return false;,因此页面会不断重新加载。通过使用 Firebug 工具,我得到了错误消息 uncaught exception: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.getAllResponseHeaders]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js :: anonymous :: line 16" data: no]
当我将链接从 <a href="">Link</a> 更改为 <a>Link</a> 时,代码正常工作(我收到了第一和第二个成功消息)。此外,如果我保留链接不变,并在 $("a").click 代码的末尾添加 return false;,代码也能正常工作。

谢谢!感谢你让它听起来像我不是傻瓜 :) - codefinger

1
Error: uncaught exception: 
[Exception... "prompt aborted by user"  nsresult: "0x80040111 
(NS_ERROR_NOT_AVAILABLE)"  location: 
"JS frame :: resource:///components/nsPrompter.js :: openTabPrompt :: line 457"  data: no]

当我同时将onclick事件和一个live href url附加到锚点标签时,出现了这个错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接