4 * Copyright, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://www.tinymce.com/license
8 * Contributing: http://www.tinymce.com/contributing
12 * This class enables you to use JSON-RPC to call backend methods.
14 * @class tinymce.util.JSONRequest
16 * var json = new tinymce.util.JSONRequest({
17 * url: 'somebackend.php'
22 * method: 'someMethod1',
24 * success: function(result) {
25 * console.dir(result);
31 * method: 'someMethod2',
33 * success: function(result) {
34 * console.dir(result);
38 define("tinymce/util/JSONRequest", [
42 ], function(JSON, XHR, Tools) {
43 var extend = Tools.extend;
45 function JSONRequest(settings) {
46 this.settings = extend({}, settings);
51 * Simple helper function to send a JSON-RPC request without the need to initialize an object.
52 * Consult the Wiki API documentation for more details on what you can pass to this function.
56 * @param {Object} o Call object where there are three field id, method and params this object should also contain callbacks etc.
58 JSONRequest.sendRPC = function(o) {
59 return new JSONRequest().send(o);
62 JSONRequest.prototype = {
64 * Sends a JSON-RPC call. Consult the Wiki API documentation for more details on what you can pass to this function.
67 * @param {Object} args Call object where there are three field id, method and params this object should also contain callbacks etc.
69 send: function(args) {
70 var ecb = args.error, scb = args.success;
72 args = extend(this.settings, args);
74 args.success = function(c, x) {
77 if (typeof(c) == 'undefined') {
79 error : 'JSON Parse error.'
84 ecb.call(args.error_scope || args.scope, c.error, x);
86 scb.call(args.success_scope || args.scope, c.result);
90 args.error = function(ty, x) {
92 ecb.call(args.error_scope || args.scope, ty, x);
96 args.data = JSON.serialize({
97 id: args.id || 'c' + (this.count++),
102 // JSON content type for Ruby on rails. Bug: #1883287
103 args.content_type = 'application/json';