/
|
/|
_______I_I_I_________/ |
D====/ ____________________ | - -
|| __| | | |___ || \ |
\\__[_=|_[[|_|==_]_// \|
\_________________/ | \
= = =
Satchmo.coffee
(c) 2012 Reprovinci Internetdiensten bv
Satchmo is freely distributable under the MIT license.
Portions of Satchmo are inspired or borrowed from cmlenz's
jQuery Ajax transport plugin.
The Satchmo jQuery plugin implements two methods
for the asynchronous submission of forms. The first is used when the browser supports the XMLHttpRequest 2 API or when
the form contains no file fields. The fallback transparently submits the form using a hidden <iframe>.
The source for Satchmo is available on Github, and released under the MIT license.
To use this plugin, just submit your form using jQuery's $.ajax():
var xhr = $.ajax({ satchmo: $("#myform") });
You can listen for progress events by calling xhr.progress(<callback>). Your callback will receive a
XMLHttpRequestProgressEvent which exposes the following properties:
boolean lengthComputable
long loaded # Bytes loaded until now
long total # Total of bytes that will be sent
Nota bene:
loadedandtotalmay only be used whenlengthComputableistrue.
As the iframe transport layer does not have access to the HTTP headers of the server response, it is not as simple to
make use of the automatic content type detection provided by jQuery as with regular XHR.
Another problem with using an iframe for file uploads is that it is impossible for the javascript code to determine the
HTTP status code of the servers response. Effectively, all of the calls you make will look like they are getting
successful responses, and thus invoke the done() or complete() callbacks.
If you can't set the expected response data type (for example because it may vary), you will need to employ a workaround
on the server side.
In case the iframe transport layer kicks in, an additional Satchmo__wrap variable is sent along with the request to
tell the server this is an emulated AJAX request. When this variable is present, you may choose to wrap your response
in an HTML document. This document should conform to the following specification:
HTTP/1.x 200 OK
Content-Type: text/html; charset=<charset_here>
<!DOCTYPE html>
<body status="404">
<ul>
<li>Content-Type: application/json</li>
</ul>
<pre>{"data":{"42":"So long ↵
and thanks for all the fish!"}}</pre>
</body>
The status attribute is optional, as is the <ul> element.