324
/** Builds a multipart_formdata string from an args object. Similar to
325
* make_query_string, but it returns data of type "multipart/form-data"
326
* instead of "application/x-www-form-urlencoded". This is good for
327
* encoding large strings such as text objects from the editor.
328
* Should be written with a Content-Type of
329
* "multipart/form-data, boundary=<boundary>".
330
* All fields are sent with a Content-Type of text/plain.
331
* \param args Args object as described in parse_url.
332
* \param boundary Random "magic" string which DOES NOT appear in any of
333
* the argument values. This should match the "boundary=" value written to
334
* the Content-Type header.
335
* \return String in multipart/form-data format.
337
function make_multipart_formdata(args, boundary)
342
var extend_data = function(arg_key, arg_val)
344
/* FIXME: Encoding not supported here (should not matter if we
345
* only use ASCII names */
346
data += "--" + boundary + "\n"
347
+ "Content-Disposition: form-data; name=\"" + arg_key
352
for (var arg_key in args)
354
arg_val = args[arg_key];
355
if (arg_val instanceof Array)
356
for (var i=0; i<arg_val.length; i++)
358
extend_data(arg_key, arg_val[i]);
361
extend_data(arg_key, arg_val);
364
data += "--" + boundary + "--\n";
323
369
/** Converts a list of directories into a path name, with a slash at the end.
324
370
* \param pathlist List of strings.
325
371
* \return String.
348
394
* \param path URL path to make the request to, within the application.
349
395
* \param args Argument object, as described in parse_url and friends.
350
396
* \param method String; "GET" or "POST"
397
* \param content_type String, optional. Only applies if method is "POST".
398
* May be "application/x-www-form-urlencoded" or "multipart/form-data".
399
* Defaults to "application/x-www-form-urlencoded".
351
400
* \return An XMLHttpRequest object containing the completed response.
353
function ajax_call(app, path, args, method)
402
function ajax_call(app, path, args, method, content_type)
404
if (content_type != "multipart/form-data")
405
content_type = "application/x-www-form-urlencoded";
355
406
path = make_path(path_join(app, path));
408
/* A random string, for multipart/form-data
409
* (This is not checked against anywhere else, it is solely defined and
410
* used within this function) */
411
var boundary = "48234n334nu7n4n2ynonjn234t683jyh80j";
357
412
var xhr = new XMLHttpRequest();
358
413
if (method == "GET")
369
424
/* POST sends the args in application/x-www-form-urlencoded */
370
425
url = encodeURI(path);
371
426
xhr.open(method, url, false);
372
xhr.setRequestHeader("Content-Type",
373
"application/x-www-form-urlencoded");
374
var message = make_query_string(args);
428
if (content_type == "multipart/form-data")
430
xhr.setRequestHeader("Content-Type",
431
"multipart/form-data, boundary=" + boundary);
432
message = make_multipart_formdata(args, boundary);
436
xhr.setRequestHeader("Content-Type", content_type);
437
message = make_query_string(args);
375
439
xhr.send(message);