Created
October 9, 2009 11:40
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!