Search code examples
jsonajaxdb2fastcgirpgle

CGI API to POST using $.ajax() instead of $.getJSON() RPGLE IBM i PASE environment


What CGI API can you use to replace $.getJSON() with $(ajax)...POST. GET is used for everything (ie: GET, PUT, POST, DELETE) when using CGIDEV2 in an IBM i environment? I dont want to pass my parameters the traditiontal way eg: $('[name="EMAIL"]').val() rather I want to pass JSON object string eg: {"form" : [{ "email": "yardpenalty@yahoo.com"}]}.

We are able to perform PostToGet callbacks using the $.getJSON() using CGIDEV2 but I can't use $.ajax in its fullest. Here is what we do now which is all GET requests

PHP/JS:

// load all parameters
data =  'INSTANCE=<?echo trim($PATH_INSTANCE)?>' +
        '&FUNCTION=<?echo urlencode(trim($FUNCTIONCODE))?>' +
        '&USER=' + $('[name="USER"]').val() +
        '&CONTACT=' + w$('[name="CONTACT"]').val() +  
        '&EMAIL=' + $("input[name='EMAIL']").val() +
        '&MSG=' + $('[name="MSG"]').val() + 
        '&TYPE=' +  $('[name="TYPE"]').val();

  // Call the RPG REST JSONP program 
    $.getJSON( "http://www.domain.com:8082/rest/RPGLEPGM?callback=?",data )
          .done(function( json ) {  ... }

  //Domain is actually
   http://www.domain.com:8081

RPGLE PGM:

Begsr $Incoming;

         cgiPostToGet(); // Convert POST to GET

         callback = cgiParseGet('callback'); // callback 

         p#Function = cgiParseGet('FUNCTION');      
Endsr;    

But I want to be able to use the other AJAX methods doing various actions such as simply updating records .post()/.ajax() on the fly or simple .get() ajax calls without creating a callback. I don't want to have to use getJSON every time I use ajax not to mention its bad practice to POST on a GET, but from what I understand the .getJSON() provides JSONP functionality while the others do not by default.

EDIT: We do have our ajax RPGLE PGMS on a different port than the actual website so JSONP is necessary and the client knows its JSONP because we pass the callback function back.


Solution

  • Unfortunately you can POST using JSONP but you cannot read the header response.

    I FINALLY found the solution to executing both db2 and ajax requests on a single DOMAIN.

    SOLUTION HERE

    If you need to perform AJAX requests on the SAME DOMAIN in an IBMi environment w/apache/PASE you need the following ScriptAlias and two directives in your http.conf file:

    ScriptAliasMatch /rest/([0-9a-zA-Z]+$) /qsys.lib/rest.lib/$1.pgm
    
    <Directory /qsys.lib/obj.lib/>
      order allow,deny
      allow from all
       Header Always Set Access-Control-Allow-Origin *
       Options +ExecCGI +Includes -Indexes +MultiViews
       CGIConvMode %%MIXED/MIXED%%
    </Directory>
    
     <Directory /qsys.lib/rest.lib> 
        CGIConvMode %%EBCDIC/EBCDIC%%       
        Order Allow,Deny           
        Allow From all         
        Header Always Set Access-Control-Allow-Origin * 
        Options +ExecCGI -FollowSymLinks -SymLinksIfOwnerMatch +Includes -IncludesNoExec -Indexes -MultiViews       
     </Directory>
    

    rest.lib member is where all of the ajax rpgle pgms reside.

    obj.lib is where all CALL WEBXXX for db2 calls in PHP are located.

    Now you are able to call AJAX requests under the /rest/$1PGM pattern.

    For instance:

    $.ajax({
           type: "POST",
           url:  "http://www.domain.com/rest/WEB055S",
           data:  obj,
           contentType: "application/json; charset=utf-8"
           ).
           done(function(data){
                   $("#signupForm .loading").addClass("hidden");
                   //Successful Submit! 
                   if(typeof data.MESSAGE != "undefined"){  
                        clicked = 0;
                        $("#ok_msg").html(data.MESSAGE).show();      
                   }
                   else{//ERROR
                     //console.log(data.ERROR);
                    $("#attendee #hth_err_msg").html(data.ERROR).show();
                    $('.add-attendee').addClass('hidden');
                    $('.edit-attendee').addClass('hidden');
                   }  
           }).
           fail(function(){
              $("#hth_err_msg").html("We're sorry, you're request cannot be processed at this time. It is likely the corporate system is under maintenance.").show();
           });