Created
August 1, 2014 11:20
-
-
Save gtheys/e7136b7820ad41bc4242 to your computer and use it in GitHub Desktop.
Issue sinatra returning response object instead of json
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 "rubygems" | |
require "sinatra/base" | |
require 'pony' | |
require 'yaml' | |
require 'json' | |
class Contact < Sinatra::Base | |
before do | |
p"START LOGGING" | |
response.headers["Access-Control-Allow-Origin"] = "*" | |
response.headers["Access-Control-Allow-Methods"] = "POST" | |
p response.headers.inspect | |
end | |
# Load the configuration file for the mailserver settings | |
conf = YAML.load_file('configuration.yml') | |
post '/' do | |
content_type :json | |
p params.inspect | |
if params[:email].empty? | |
response[:status] = "error" | |
response[:message] = "No email" | |
p "json: #{response.to_json}" | |
body response.to_json | |
elsif params[:mailbody].empty? | |
response[:status] = "error" | |
response[:message] = "No message?" | |
response.to_json | |
elsif | |
Pony.mail( | |
:from => params[:email] + "<" + params[:email] + ">", | |
:to => 'geert.theys@gmail.com', | |
:subject => params[:email] + " has contacted you", | |
:body => params[:mailbody], | |
:via => :smtp, | |
:via_options => { | |
:address => conf['smtp'], | |
:port => conf['port'], | |
:user_name => conf ['user_name'], | |
:password => conf ['password'], | |
:enable_starttls_auto => true, | |
:authentication => :plain | |
}) | |
response[:status] = "success" | |
response[:message] = "Email sent. We will contact you as soon as possible" | |
p response.inspect | |
response.to_json | |
end | |
end | |
end |
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
// Contact form submission | |
$(document).ready(function() { | |
jQuery("#clickme").click( function() { | |
$('body').css('cursor', 'wait'); | |
var form=$('#contactForm'); | |
$.ajax({ | |
type: "POST", | |
url: "http://cooldevops_contact.dev/", | |
data: form.serialize(), | |
dataType: "json", | |
success: function(msg) { | |
$("#formResponse").removeClass('error'); | |
$("#formResponse").removeClass('success'); | |
$("#formResponse").addClass(msg.status); | |
$("#formResponse").html(msg.message); | |
}, | |
error: function() { | |
$("#formResponse").removeClass('success'); | |
$("#formResponse").addClass('error'); | |
$("#formResponse").html("There was an error submitting the form. Please try again."); | |
} | |
}); | |
$('body').css('cursor', 'default'); | |
}); | |
}); |
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
"START LOGGING" | |
"{\"Content-Type\"=>nil, \"Access-Control-Allow-Origin\"=>\"*\", \"Access-Control-Allow-Methods\"=>\"POST\"}" | |
"{\"email\"=>\"\", \"mailbody\"=>\"\"}" | |
"json: \"#<Sinatra::Response:0x007fed1cec5cc8>\"" | |
127.0.0.1 - - [01/Aug/2014 13:10:55] "POST / " 200 39 0.0079 |
canton7
commented
Aug 1, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment