Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- javascript
- futile attempts to create a generic PHP-JS communications library
- input: mixed
- output: JSON (as simple as a string)
- "val" is always the result node
- "error" is sometimes supplied if there IS an error
- */
- function jdx () {
- // javascript data exchange protocol
- // default is POST, but GET works too
- var i; // variable i is locally reused (warn)
- var v = []; // blank val array
- for( var i = 1; i < arguments.length; i++ ) {
- // variable i is locally reused
- v[i-2] = arguments[i]; // transfer params to values
- }
- jQuery.ajax({
- async: false, // !important;
- cache: false, // !important;
- global: false,
- url: ".ajax.php",
- type: "POST", // just cos it has no length limit
- dataType: "json",
- data: ({
- req : arguments[0],
- val : v.join('!!JOIN!!') // meh
- }),
- success: function(data) {
- i = data.val;
- }
- });
- return i;
- }
- <?php
- /*
- PHP
- futile attempts to create a generic PHP-JS communications library
- input: REST
- output: JSON (as simple as a string)
- */
- header('Cache-Control: no-cache, must-revalidate');
- header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // IE bug: caches JSON
- header('Content-type: application/json'); // you return JSON
- if ($_GET) {
- $args = $_GET; // heh, just giving it a name
- } else {
- $args = $_POST; // POST-compatible?
- }
- $req = $args['req']; // what the client is requesting
- $val = $args['val']; // anything the client wants to specify
- try {
- $ret = @call_user_func_array ($req, explode('!!JOIN!!', $val));
- echo '{"val": "' . $ret . '"}';
- } catch (Exception $e) { // just in case
- echo ('{"val": "",
- "error": ' . $e->getMessage() . '}');
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement