ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Calling hidden templates from AJAX call

July 18, 2012 1:13pm

Subscribe [1]
  • #1 / Jul 18, 2012 1:13pm

    a la mode

    168 posts

    I have a question about hidden templates, embeds, and script calls. Basically, I have a .NET web service that I need to call from EE1 to populate available markets being maintained by a third party business source. In order to do this efficiently, I wrote a simple PHP file that handles the information transaction. Something like this:

    <?php
        $marketBitmask = isset($_POST['marketBitmask'])?$_POST['marketBitmask']:NULL;
        $client = new SoapClient("https://webAddress/MarketingData.asmx?WSDL");
        $result = $client->TheWsdlMethod(array(
            'marketBitmask'=>$marketBitmask
        ));
        $return[] = $result;
        echo json_encode($return);
    ?>


    Then I process that through a jQuery AJAX call like:

    var market = $(".ddl_productList").attr('title');
    var marketBitmask = '';
    switch(market){
        case 'Appraiser':
            marketBitmask = 2;
            break;
        default:
            marketBitmask = 'NULL';
            break;
    }
    $.post("services/.products", { marketBitmask: marketBitmask.toString() }, function(data){
        // Populate Results on Return
        $.each(data, function(n, val){
            var products = val.ProductsResult.Products;
            var result = val.ProductsResult.Result;
            var resultDescription = val.ProductsResult.ResultDescription;
            if(result!='Success'){
                // Post Error
                $(".noError").hide();
                $("#ggspr_result").val(result).css('color','red');
                $("#ggspr_resultDescription").val(resultDescription).css('color','red');
                return;
            }else{
                // Populate Results
                $(".noError").show();
                $.each(products.Product, function(i, product){
                    $("#ddl_productList").append("<option value='"+product._ProductId+"'>"+product._Name+"</option>");
                });
                return;
            }
        });
    }, "json");


    The problem I am running into is that I would like to keep the services template group set as a hidden template type so that no-one can hit them directly, but I need to be able to call them from a script like shown above.

    I can use the hidden templates as embeds such as:

    {embed="services/.products"}


    But that doesn’t help…  😉

    Thanks guys!
    Brian

  • #2 / Jul 19, 2012 11:17am

    Kevin Smith

    4784 posts

    Hi Brian,

    Yep, that’s not going to work for the same reason that users can’t bring up the template in a browser. You could do one of two things, and I’d probably opt for the first:

    1. Instead of a hidden template, just use a regular PHP file outside of EE or create a simple module to handle it (so that it’s still part of the EE system).

    2. Instead of making the template hidden, send a secret hash along with the AJAX post call to authenticate it. In the products template, show a 404 unless the request is a POST request that contains the secret hash.

    Would one of those two things work?

  • #3 / Jul 20, 2012 5:51pm

    a la mode

    168 posts

    Thanks for the direction Kevin!  😉

  • #4 / Jul 23, 2012 5:59pm

    Kevin Smith

    4784 posts

    Glad I could help, Brian! Have a good one.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases