Moving SCORM API from XAJAX to jQuery

Yesterday I finished a 20 hours job of moving the SCORM API in Dokeos 1.8.6 from XAJAX to jQuery. The problem with XAJAX is that version 0.5 represents an important change in comparison to version 0.2, that version 0.5 is the only one implementing synchronous AJAX calls, and that upgrading was obviously going to take me some time (possibly very long). We also decided to go with jQuery uniformally in Dokeos 1.8 and 2.0, so I decided, following a suggestion by Eric Marguin, to move it to jQuery. The problem there is that, if jQuery allows you to execute GET/POST requests easily, the documentation isn't clear as to how to pass array variables to the resulting PHP script. So I had to implement a array-serializer of my own. The result looks something like this ugly piece of code:
params=''; params += 'lid='+lms_lp_id+'&uid='+lms_user_id+'&vid='+lms_view_id; params += '&iid='+lms_item_id; obj_string = ''; for (i in item_objectives){ obj_string += '&objectives['+i+']='; obj_temp = '['; for (j in item_objectives[i]) { obj_temp += item_objectives[i][j]+','; } obj_temp = obj_temp.substr(0,(obj_temp.length-2)) + ']'; obj_string += encodeURIComponent(obj_temp); } params += obj_string; $.ajax({ type: "GET", data: params, url: "lp_ajax_save_objectives.php", dataType: "script", async: false });
Where item_objectives is an indexed array. All in all, the implementation seems to work much better than the previous one (because the async: false param allows me to set the AJAX query as synchronous), but I still want to test it a little more before integrating it into Dokeos. For the curious about the changes applied, they can be seen in an SVN branch: http://dokeos.svn.sourceforge.net/viewvc/dokeos?view=rev&revision=17130

Comments

Hi Yannick,

Thought I'd tune in considering I faced similar issues when trying to use jQuery for specific parts of Dokeos 2.0. Given the possibilities AJAX provides in jQuery it really is a shame that the documentation is somewhat lacking at times. Luckily Google proves to be an essential programmer's friend each and every time.

That being said jQuery actually has built in serialization as far as AJAX posts and gets go. I can't imagine why they don't mention this in the documentation. I'll illustrate things based on an example situation we faced recently: modal dialogs to (hopefully) confirm whether or not an action taken via jQuery / AJAX succeeded. This meant we had to get some language variables into the scripts.

This required us to pass on both the variable to be translated as well as the application we're working in. So that's two seperate variables we had to get into the AJAX post. After a lot of research , I found out that the data parameter of the ajax-function can take multiple variables. I'll just copy paste the entire statement here:

function translation(string, application) {

var translated_string = $.ajax({
type: "POST",
url: "./common/javascript/ajax/translation.php",
data: { string: string, application: application },
async: false
}).responseText;

return translated_string;
};

So basically all you need is this: data: { string: string, application: application }, it's the {} that makes it work. In this case the internal variables happen to have the same name as those which will be used for the post- and/or get)variables. According to the documentation jQuery automatically serializes the variables when you want to use an AJAX GET instead of a POST.

I've done some limited tests and the translation function also seems to work when I switch from POST to GET.

Hans De Bisschop
Erasmus University College Brussels

PS.

If you want to use an array inside of that array, I found the JSON.Stringify library (http://www.JSON.org/json2.js) to come in extremely handy. It converts the array to a valid JSON-object. And since PHP 5.2(+) has excellent support for JSON, you can get the array in your PHP script by doing the following:

$data = $_GET['data'];
$res = json_decode(stripslashes($data), true);