538
538
return str.substr(str.length - substring.length) == substring;
541
/** Equivalent to Python's repr.
542
* Gets the JavaScript string representation.
543
* Actually just calls JSON.stringify.
547
return JSON.stringify(str);
541
550
/** Removes all occurences of a value from an array.
543
552
Array.prototype.removeall = function(val)
611
620
return str.join('');
614
/** Makes an XMLHttpRequest call to the server.
623
/** Makes an asynchronous XMLHttpRequest call to the server.
615
624
* Sends the XMLHttpRequest object containing the completed response to a
616
625
* specified callback function.
618
627
* \param callback A callback function. Will be called when the response is
619
628
* complete. Passed 1 parameter, an XMLHttpRequest object containing the
620
* completed response. If callback is null this is a syncronous request
621
* otherwise this is an asynchronous request.
629
* completed response.
622
630
* \param app IVLE app to call (such as "fileservice").
623
631
* \param path URL path to make the request to, within the application.
624
632
* \param args Argument object, as described in parse_url and friends.
625
* \param method String; "GET", "POST", "PUT", or "PATCH"
626
* \param content_type String, optional.
633
* \param method String; "GET" or "POST"
634
* \param content_type String, optional. Only applies if method is "POST".
635
* May be "application/x-www-form-urlencoded" or "multipart/form-data".
627
636
* Defaults to "application/x-www-form-urlencoded".
629
638
function ajax_call(callback, app, path, args, method, content_type)
640
if (content_type != "multipart/form-data")
632
641
content_type = "application/x-www-form-urlencoded";
633
642
path = app_path(app, path);
637
646
* used within this function) */
638
647
var boundary = random_string(20);
639
648
var xhr = new_xmlhttprequest();
640
var asyncronous = callback != null;
643
xhr.onreadystatechange = function()
649
xhr.onreadystatechange = function()
651
if (xhr.readyState == 4)
645
if (xhr.readyState == 4)
651
656
if (method == "GET")
653
658
/* GET sends the args in the URL */
654
659
url = build_url({"path": path, "args": args});
655
660
/* open's 3rd argument = true -> asynchronous */
656
xhr.open(method, url, asyncronous);
661
xhr.open(method, url, true);
661
/* POST & PUT & PATCH sends the args in the request body */
666
/* POST sends the args in application/x-www-form-urlencoded */
662
667
url = encodeURI(path);
663
xhr.open(method, url, asyncronous);
668
xhr.open(method, url, true);
665
670
if (content_type == "multipart/form-data")
668
673
"multipart/form-data; boundary=" + boundary);
669
674
message = make_multipart_formdata(args, boundary);
671
else if (content_type == "application/x-www-form-urlencoded")
673
678
xhr.setRequestHeader("Content-Type", content_type);
674
679
message = make_query_string(args);
676
else if (content_type == "application/json")
678
xhr.setRequestHeader("Content-Type", content_type);
679
message = JSON.stringify(args);
683
xhr.setRequestHeader("Content-Type", content_type);
686
681
xhr.send(message);
688
/* Only return the XHR for syncronous requests */
695
/** Attempts to JSON decodes a response object
696
* If a non-200 response or the JSON decode fails then returns null
698
function decode_response(response)
700
if (response.status == 200)
704
var responseText = response.responseText;
705
return JSON.parse(responseText);