Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.94 KB | None | 0 0
  1. /*
  2.    javascript
  3.    futile attempts to create a generic PHP-JS communications library
  4.    input:  mixed
  5.    output: JSON (as simple as a string)
  6.            "val"    is always the result node
  7.            "error"  is sometimes supplied if there IS an error
  8. */
  9.  
  10. function jdx () {
  11.     // javascript data exchange protocol
  12.     // default is POST, but GET works too
  13.     var i; // variable i is locally reused (warn)
  14.     var v = []; // blank val array
  15.     for( var i = 1; i < arguments.length; i++ ) {
  16.         // variable i is locally reused
  17.         v[i-2] = arguments[i]; // transfer params to values
  18.     }
  19.     jQuery.ajax({
  20.         async: false, // !important;
  21.         cache: false, // !important;
  22.         global: false,
  23.         url: ".ajax.php",
  24.         type: "POST", // just cos it has no length limit
  25.         dataType: "json",
  26.         data: ({
  27.             req : arguments[0],
  28.             val : v.join('!!JOIN!!') // meh
  29.         }),
  30.         success: function(data) {
  31.             i = data.val;
  32.         }
  33.     });
  34.     return i;
  35. }
  36.  
  37.  
  38.  
  39.  
  40. <?php
  41.     /*
  42.        PHP
  43.        futile attempts to create a generic PHP-JS communications library
  44.        input:  REST
  45.        output: JSON (as simple as a string)
  46.     */
  47.     header('Cache-Control: no-cache, must-revalidate');
  48.     header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // IE bug: caches JSON
  49.     header('Content-type: application/json'); // you return JSON
  50.    
  51.     if ($_GET) {
  52.         $args = $_GET; // heh, just giving it a name
  53.     } else {
  54.         $args = $_POST; // POST-compatible?
  55.     }
  56.  
  57.     $req = $args['req']; // what the client is requesting
  58.     $val = $args['val']; // anything the client wants to specify
  59.        
  60.     try {
  61.         $ret = @call_user_func_array ($req, explode('!!JOIN!!', $val));
  62.         echo '{"val": "' . $ret . '"}';
  63.     } catch (Exception $e) { // just in case
  64.         echo ('{"val": "",
  65.                "error": ' . $e->getMessage() . '}');
  66.     }
  67. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement