Skip to content

Instantly share code, notes, and snippets.

@mr-rock
Created October 9, 2009 11:40
Show Gist options
  • Save mr-rock/205948 to your computer and use it in GitHub Desktop.
Save mr-rock/205948 to your computer and use it in GitHub Desktop.
An example of Sinatra working with Ajaxified JQuery based on some pieces of code published by Rafael George on the Sinatra Google Group.
require 'sinatra'
require 'dm-core'
require 'haml'
DataMapper.setup(:default, 'sqlite3::memory:')
class Message
include DataMapper::Resource
property :id, Serial
property :name, String
property :message, String
end
Message.auto_migrate!
get '/' do
haml :app
end
post '/new' do
@message = Message.new
@message.message = "#{params[:will]} will #{params[:you]} you"
@message.save
@message.message
end
__END__
@@ app
%html
%head
%title Sinatra JQuery Test
%script{:type => 'text/javascript', :src => 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'}
:javascript
$(document).ready(function() {
$('#new_message').submit(function() {
$.post($(this).attr('action'), $(this).serialize(), function(data){
$('#message').append("<p>" + data + "</p>");
$('#new_message').each(function(){this.reset();});
}, "text");
return false;
});
});
%body
#footer
%h2 leave your message
#message
%form#new_message{:action => '/new'}
%p
%input.pink{:type => "text", :id => "will", :name => "will"}
%strong will
%input.pink{:type => "text", :id => "you", :name => "you"}
%strong you
%input{:type => "submit", :value => "post!"}
@mol90
Copy link

mol90 commented May 2, 2013

Hi, I'm doing a client (in javascript/jquery) and a server (in sinatra) that returns json object to clients when they do request html via $.get (). My problem is that I obtain the next error in the console's chrome (it also happen in other browsers):

XMLHttpRequest cannot load ..... Origin null is not allowed by Access-Control-Allow-Origin.

I know it's a security problem. I have tried to fix it with several solutions suggested in forums, but they don't work.
Could I tell me if you have been this problem? and how you fix it?

Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment