It's hard for me to make jquery.form with cross-domain query. I have problems with Firefox and Chrome (not even trying IE).
Explanation: our entire site is located at http://www.mysite.com . However, my contact form is located on another server that http://contact.mysite.com links to . I thought that including it in a subdomain would circumvent the problems associated with cross-domain requests, but apparently this did not happen. http://contact.mysite.com is implemented in Sinatra .
My javascript setting does not represent anything. The form action points to http://contact.mysite.com , and the POST method:
<form id="contact" action="http://contact.mysite.com/" method="post">
jquery.form is configured with ajaxForm call :
$(document).ready(function() {
$('#contact').ajaxForm({
success: function() { $('#success').fadeIn("slow"); },
error: function() { $('#error').fadeIn("slow"); }
});
});
The first problem I ran into was with Firefox 3.5 - apparently it is sending an OPTIONS request waiting for a specific response from the server. I used this question to configure my Sinatra application to do what was expected (it seems that more recent versions of Sinatra include the option verb):
require 'rubygems'
require 'sinatra'
require 'pony'
configure do
class << Sinatra::Base
def options(path, opts={}, &block)
route 'OPTIONS', path, opts, &block
end
end
Sinatra::Delegator.delegate :options
end
options '/' do
response['Access-Control-Allow-Origin'] = '*'
response['Access-Control-Allow-Methods'] = 'POST'
response['Access-Control-Max-Age'] = '2592000'
end
post '/' do
Pony.mail(...)
end
With jQuery 1.4.3, I saw an OPTIONS request in firebug, followed by a POST request (status 200. The message was sent). With jquery 1.3.2 or 1.5, only the OPTIONS request was shown (the message was not sent).
However, the callback erroralways starts with all versions of jquery I've tried. I traced this to a call $.ajax(...), so I'm not sure if this problem comes from jquery.form or jquery.
:
$('#contact').ajaxForm({
success: function() { $('#success').fadeIn("slow"); },
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR.status);
console.log(jqXHR.statusText);
}
});
jQuery 1.4.3 ( OPTIONS POST 200):
0
(empty string)
jquery 1.5 ( OPTIONS 200, POST )
302
error
.
.