1

I have a very simple sinatra site that I'm trying to access via ajax through jQuery.

To keep things incredibly simple, this code snippet:

get '/behavior_count' do
  "60"
end

which returns "60" in the browser, shows up as an empty string when attempting to access the site via $.get in jQuery. The strange part is in Firebug, while the Response is empty, the HTTP header correctly describes Content-Length as 2, and I can see the request show up on the server.

Is there something specific to Sinatra that isn't returning my data, or am I not using jQuery correctly?

If it helps, I also tried this code:

get '/behavior_count' do
  content_type 'text/plain', :charset => 'utf-8'
  "60"
end

and my jQuery looks like

$.get('http://mysite:4567/behavior_count'); // Ignore the response, but
                                            // watch the request in firebug

Any ideas?

jerhinesmith
  • 15,214
  • 17
  • 62
  • 89

2 Answers2

3

Until someone comes up with a proper answer, here's the minimal example I tried and it works:

test.rb:

require 'rubygems'
require 'sinatra'

get '/' do
  haml :test
end

get '/howmany' do
  "42"
end

views/test.haml:

%html
  %head
    %script{:type => "text/javascript", :src => 'js/jquery.js'}
    :javascript
      $(document).ready(function() {
        $('#btn').click(function(event){
          $.get('/howmany', function(data) {
            $('#answer').html(data);
          });
        });
      });
    %title Test page

  %body
    %input#btn{:type => 'button', :value => 'How many?'}
    #answer

(there's also public/js/jquery.js, of course)

Mladen Jablanović
  • 43,461
  • 10
  • 90
  • 113
2

It's probably not a Sinatra / Jquery interaction problem, instead, it's an Ajax "cross domain security" problem.

Your original problem could be due to the fact that you've got your form and your server hosted on different domains. XMLHTTPRequests such as .get, .post, .ajaxForm and .ajaxSubmit, will fail if they are on different domains. Check your logs on the app that receives the post, and you might see something like "OPTIONS behavior_count" 404 in the log file. Basically, it will work when you hit the app directly, but when you try to use AJax to do it, and that Ajax is accessing it from a different domain, i.e., the "action" option on the form has "http://some.differentdomain.com/behavior_count" in it.

That would explain why your simple example works, because the form and the post are happening on the same application / same domain.

I had the same problem for five hours just now; I wanted to use a generic "comments" application and have other applications be able to post to the one central application, on a different domain. It won't work. But then I made the two applications into one, and everything was fine. Alternately, you can try to use JSONP to make it work and still keep the two applications separate.

I read "jQuery $.ajax(), $.post sending “OPTIONS” as REQUEST_METHOD in Firefox", which helped.

Community
  • 1
  • 1
Greg Edwards
  • 598
  • 1
  • 5
  • 12
  • It's been a while, but I'm pretty sure that was the problem. It was just something I was playing around with, and I eventually abandoned it, but it was certainly going across domains. I didn't realize that Ajax didn't allow that. – jerhinesmith Jan 14 '11 at 16:56
  • JSONP was invented to get around this problem. See the notes on http://api.jquery.com/jQuery.get/ Some additional background here http://en.wikipedia.org/wiki/Same_origin_policy – Eli Mar 25 '13 at 20:19