Hacker for Hire

Sajax With JSON Support In PHP

Wyatt •

A while ago, I wanted to recreate the cool functionality you get with DWR in PHP. There are a ton of frameworks out there that come close, but miss completely. Thus, I finally caved and created my own solution that is nothing more than patching the existing software into my desired hack. From the simple ingredients of:

Hacked-SAJAX was born.

Example code is included and it can be used as follows:

function addNameToObject($obj) {
   $obj->name = "wyatt " . $obj->name;
   return $obj;


sajax_init();</div> </div>

var myObj = { name: "neal", height: 6 };</p>

x_addNameToObject(myObj, function (obj2) {console.log(obj2); } );</div> </div>

This is of course released under the GPL or whatever means that I’m giving away my modifications, please cite everyone who had a part in it. My changes to the code base are as follows

//parse out the args array
$newArgArray = array();
$arraySize = sizeof($args);
for($i = ; $i < $arraySize; $i++) {
        //decode the JSON object
        $tmpArg = json_decode(stripslashes($args[$i]));
        array_push($newArgArray, $tmpArg);

$result = call_user_func_array($func_name, $newArgArray);
//encode the result in to JSON
$retString = json_encode($result);
//echo "var res = " . trim(sajax_get_js_repr($result)) . "; res;";
echo "var res = " . $retString . "; res;"

for (i = ; i < args.length1; i++) {
        //uri += "&rsargs[]=" + escape(args[i]);
        uri += "&rsargs[]=" + escape(args[i].toJSONString());

for (i = ; i < args.length1; i++) {
        //post_data = post_data + "&rsargs=" + esscape(args[i]);
post_data = post_data + "&rsargs[]=" + escape(args[i].toJSONString());

}</div> </div>

The original rant is here:

I have been scouring the Internet for two days for looking for a suitable PHP alternative to DWR(read the previous post for more information) and haven’t found a single one so I’ve hacked out my own.</p>

I had been looking at xajax in the hopes that it would get me what I was looking for, but it didn’t. So the next one I found was SAJAX. This project appears to be less active than xajax; however, disappointment strikes again. SAJAX does not support JavaScript objects, only basic variables. While that’s all well and good, when I need to pass larger objects, that’s extremely fricken annoying!!!

But wait … a glimmer of hope on the horizon … Sanjer promises to combine SAJAX and JSON. Only one major problem, it’s not actively maintained and it’s based on the 0.10 version of SAJAX which had some serialization bugs.

So to end the frustration for me and anyone else on the Internet who would ever want to do something this stupid (it apparently has to be stupid since no one else is willing to pull this off), I present my solution to the problem … hacking SAJAX. Before I delve into what changed, I’m going to give you the files so you can just run with it in the event that you aren’t interested in that crap. This zip includes everything you need to pull this off: Crockford’s json.js from http://www.json.org/, hacked up version of Sajax.php from me, and a simple index.php file that gives a super simple example of how it works. Oh yeah, since this is all based on GPL/Open source code, my derivation falls under the same licensing and comes with absolutely no warranty. Now, on to the other stuff that most people don’t really care about.

Fortunately, the people who wrote the SAJAX framework did a really good job a keeping a very nice code separation. All that had to happen to tweak this was:

  • JSON Encode the arguments before they are passed to the proxied PHP code
  • Create a new argument array of decoded JSON objects
  • Pass in the new argument array
  • JSON Encode the function result

Now I know that I could put this encode and decode inside every PHP function; however, I’m lazy and I don’t want to do that, especially if it’s a framework and it should do it for me.

Well, that’s it. Really fricken hard, eh? Yeah, didn’t think so. I’m sure that this version will work for most uses; however, if you do find a bug, feel free to leave a comment or send it to me at wyatt neal on my GMail account and I’ll do my best to help you out. Hopefully this will inspire someone else with more time to come up with a better PHP framework that is a lot closer to the coolness you get from DWR.

If you have one that does what DWR does, drop a comment and let me know. SAJAX, PAJAX, and XAJAX don’t count because I’ve looked at them and I know they don’t provide the same level of use. The only one that comes close is PAJAX but it fails because it does synchronous instead of asynchronous calls by default (what where you thinking people?!?).

[1]: http://www.modernmethod.com/sajax/ [2]: http://www.php.net [3]: http://www.json.org/ [4]: http://sajax.info [5]: http://github.com

comments powered by Disqus