PostURL

From Svg wiki

postURL() is an function that send data to a given location. It is useful for saving the results of dynamic SVGs that have been updated based on user input/interaction or timed data feeds.

Examples include an SVG image that has been modified by a user, who wants to save the end product.

from Adobe, is as follows:

postURL( url, text, callback, type, enc )
url URL to post data; for security reasons, must be on the same server as SVG document
text data to post
callback function which processes reply status and the returned contents of the file from url
type optional media type of the posted data (such as "text/plain"). Not all browsers support that.
enc optional encoding for data null, 'gzip' and 'deflate' are possible values. If browser does not support binary post this parameter is ignored.

postURL() is a non-standard extension available only with the AdobeSVGViewer.


From the Adobe SVG Forums:

Method postURL

Synopsis:

void postURL(string url, string data, viewer::AsyncStatusCallback cb, string mimeType, string contentEncoding)

Post data to the given URL (that should come from the same domain). Notify the callback when done. One can also specify MIME type to be reported to server and encoding to be used (valid values are null, "gzip" and "deflate", some browsers cannot post binary data and will ignore encoding). Callback will get passed an object of type viewer::AsyncURLStatus when post operation completes. Reply is assumed to be either ASCII or Unicode text (in UTF-8 or UTF-16 encodings), possibly compressed with gzip or deflate. Callback will get decoded, uncompressed text as a string.

For security reasons, it is possible to post only to the server that SVG document came from.


Beware that ASV considers the http://localhost and http://127.0.0.1 in a loopback as different server and will throw security violation error.


Using XMLHttpRequest to provide postURL in Firefox

adapted from code posted at http://www.jibbering.com/2002/5/dynamic-update-svg.html

/**
 * httpRequest
 * factory used for creating XMLHttpRequest objects
 */
function httpRequest()
{
    var xmlhttp;
    try {
        xmlhttp = new XMLHttpRequest();
    } catch (e) {
        xmlhttp = false;
    }
    if (!xmlhttp) {
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            if (!xmlhttp) {
                try {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    xmlhttp = false;
                }
            }
        } catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}
/**
 * postURL
 * mimics postURL function (ASV) for browsers which support XMLHttpRequest
 */
if (typeof postURL=='undefined') {
    postURL = function(url, data, callback, type, enc) {
        var xmlhttp = new httpRequest();
        if (xmlhttp) {
            xmlhttp.open("POST", url, true);
            if (enc && (enc == null || enc == 'gzip' || enc == 'deflate')) {
                xmlhttp.setRequestHeader("Content-Encoding", enc);
            }
            if (type) {
                xmlhttp.setRequestHeader("Content-Type", type);
            } else {
                xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4) {
                    callback({status:xmlhttp.status, content:xmlhttp.responseText, contentType:xmlhttp.getResponseHeader("Content-Type")});
                }
            }
            xmlhttp.send(data)
        } else {
            alert("Sorry, your browser/SVG viewer neither supports postURL nor XMLHttpRequest!");
        }
    }
}
Retrieved from "http://wiki.svg.org/PostURL"