Changeset 27322


Ignore:
Timestamp:
05/09/13 18:19:57 (8 years ago)
Author:
ak19
Message:

Added Ajax PostAsync method urlPostAsync and tested it works.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/web/script/gsajaxapi.js

    r27318 r27322  
    188188    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    189189   
    190     // If asynchronous:
    191 //    xmlHttp.onreadystatechange = function() {
    192 //        if (xmlHttp.readyState == 4) {
    193 //            updatepage(xmlHttp.responseText);
    194 //        }
    195 //    }
    196    
    197190    if(un_ != null) {
    198191    params += "&un=" + un_;
     
    208201    return xmlHttp.responseText;
    209202}
     203
     204    // New, an Ajax Asynchronous Post method.
     205    // For helpful links, see the urlPostSync() method above
     206    this.urlPostAsync = function(scriptURL, params, callback) {
     207    var xmlHttp=false;
     208       try {
     209         // Firefox, Opera 8.0+, Safari
     210         xmlHttp=new XMLHttpRequest();
     211       }
     212       catch (e) {
     213         // Internet Explorer
     214         try {
     215           xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
     216         }
     217         catch (e) {
     218           try {
     219             xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
     220           }
     221           catch (e) {
     222             alert("Your browser does not support AJAX!");
     223             return false;
     224           }
     225         }
     226       }
     227
     228
     229
     230    // e.g. scriptURL: /greenstone/cgi-bin/metadata-server.pl
     231    xmlHttp.open('POST', scriptURL, true); // true means asynchronous
     232    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     233
     234
     235    // If asynchronous:
     236    // If the callback param is a function, we will set it up to get called when
     237    // the async post has finished (is ready)
     238    // if the callback parameter isn't a function, the param represents a field
     239    // that we want to dynamically update when the async post process has finished
     240
     241    var typeof_callback = typeof(callback);
     242    if ((typeof_callback == "string") || (typeof_callback == "number") || (typeof_callback == "boolean")) {
     243        var locid = callback;
     244
     245        xmlHttp.onreadystatechange=function() {
     246        if(xmlHttp.readyState==4) {
     247            if (locelem != null) {
     248            var locelem = document.getElementById(locid);
     249           
     250            locelem.innerHTML = xmlHttp.responseText;
     251            }
     252        }
     253        }
     254    }
     255    else if (typeof_callback == "function") {
     256        xmlHttp.onreadystatechange=function() {
     257        if(xmlHttp.readyState==4) {
     258            callback(xmlHttp); // e.g. this might do: updatepage(xmlHttp.responseText);
     259        }
     260        }
     261    }
     262    else {
     263        alert("Unrecognized type of callback value: " + typeof_callback);
     264    }
     265   
     266    if(un_ != null) {
     267    params += "&un=" + un_;
     268    }
     269    if(ky_ != null) {
     270    params += "&ky=" + ky_;
     271    }
     272    //alert("Posting Async: " + scriptURL + "?" + params);
     273
     274    xmlHttp.send(params); // needs to be escaped/encoded
     275    // if synchronous, would process xmlHttp AFTER send() call, such as by
     276    // accessing xmlHttp.responseText to return that to the caller at this point.
     277}
     278
    210279
    211280    this.setLiveMetadata = function(id,metaname,metavalue)
Note: See TracChangeset for help on using the changeset viewer.