Search code examples
phpjqueryajaxnamespacesgoogle-dfp

PHP jQuery Use Namespace


First of all, is PHP's NAMESPACE and USE features compatible with making jQuery ajax requests to handle and return data?

I have set the jQuery AJAX request and PHP handlers up but the code fails at the first hurdle when I make a request to DFP's API. NAMESPACE and USE are declared at top of the php script that the jQuery is being sent to.

use Google\AdsApi\Dfp\v201611\DFPLineItems;
use Google\AdsApi\Dfp\v201611\GetAvailabilityForecast;

require 'vendor/autoload.php';

Error that comes out -

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'file does not exist'.

In short that's what happens when the namespaces aren't working when making a request.

If it is possible what tools do I need to make sure NAMESPACE and USE are respected by the jQuery request.

Is there a preference in this scenario to use a particular data type in jQuery to better handle the request like xml, json, script, or html?

    <script>
        jQuery(document).ready(function($) {            
            $(document).on('click','#pbd-alp-load-posts a',function () {
                var country = $('#country').val();;
                $.ajax({
                    type: "POST",
                    url: "<?php echo site_url() ?>/wp-content/themes/truemag-child/orders/fetch.php", 
                    data: {country:country},
                    dataType: "html",
                    success: function(response){
                        $("#response_container").append(response);
                        //$('#pbd-alp-load-posts a').blur();
                        //console.log(page*10);                         
                    }
                });
            return false;
            });
        });
      </script> 

Solution

  • The issue didn't really have anything to do with my code.

    It was the path I was using to direct the ajax request to. Because I am using a WordPress framework the request needed to be made a a relative URL using path templates within WordPress. So instead of using a absolute URL I used a WordPress based one and everything worked flawlessly.

    url: "http://127.0.0.1/wordpress/fetch-inventory/", 
    

    Instead of

    url: "<?php echo site_url() ?>/wp-content/themes/truemag-child/orders/fetch.php",