JQuery GET request on Sinatra always results in error
I am using Sinatra to host a simple service. This is my entire application:
require 'sinatra'
get '/hello' do
'Hello world!'
end
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>
<scri开发者_开发技巧pt 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>
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/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.
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.Thanks!
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]
.
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.
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]
I got this error when I had both onclick event and a live href url attached to the anchor tag.
精彩评论