在西纳特拉JQuery的GET请求,结果总是在错误是在、错误、纳特、结果

2023-09-11 01:06:07 作者:爱懂了才能遇到爱

我使用西纳特拉举办一个简单的服务。这是我的整个应用程序:

I am using Sinatra to host a simple service. This is my entire application:

require 'sinatra'

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

然后我在我的 ./公共目录,试图对我的服务的GET请求一个HTML文件:

I then have a single html file in my ./public directory that tries a GET request on my service:

<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页面的链接,我看到下面我的服务器日志:

When I click the link on my HTML page, I see the following in my server log:

0:0:0:0:0:0:0:1%0 - - [16 /月/ 2011十时37分42秒]GET /你好HTTP / 1.1200 12 0.0030   本地主机 - - [16 /二月/ 2011:10:3​​7:42 CST]GET /你好HTTP / 1.1200 12   引用者 - > /你好

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

这似乎不错,但无论我怎样努力调整的事情,我只能得到错误回调被调用 - 从来没有成功

Which seems good, but no matter how I try to tweak things I can only get the error callback to be invoked - never the success.

我有两个问题: 1)我在做什么错了? 2)我应该如何去调试这样的问题?传递到错误回调的对象只是一个对象,我也没办法让信息有关错误的。

I have two questions: 1) What am I doing wrong? 2) How should I go about debugging problems like this? The object passed to the error callback is just an Object, and I have no way of getting info about the error.

谢谢!

推荐答案

在我的测试中,我是有问题的点击$ C $下的链接没有一个返回false; ,所以页面会保持重装。与萤火虫,我得到了错误未捕获的异常:[异常...组件返回故障code:0x80040111(NS_ERROR_NOT_AVAILABLE)nsIXMLHtt prequest.getAllResponseHeaders]nsresult:0x80040111(NS_ERROR_NOT_AVAILABLE )的位置:JS框架:: 的http://阿贾克斯。 googleapis.com/ajax/libs/jquery/1.5/jquery.min.js ::匿名:: 16行的数据:无]

During my testing I was having problems with the click code for the link not having a return false;, so the page would keep reloading. With Firebug, I got the error 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].

当我从&LT链接; A HREF=&GT;链接&LT; / A&GT; &LT; A&GT;链接&LT; / A&GT; 中,code的工作(我得到了第一和第二的成功消息)。另外,如果我离开的链路,被并添加返回false; $(A),点击 code时,code也工作。

When I changed the link from <a href="">Link</a> to <a>Link</a>, the code worked (I got both first and second success messages). Also, If I left the link as-is and added return false; to the end of the $("a").click code, the code also worked.