简体   繁体   English

PHP jQuery使用命名空间

[英]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? 首先,PHP的NAMESPACEUSE功能是否与发出jQuery ajax请求以处理和返回数据兼容?

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. 我已经设置了jQuery AJAX请求和PHP处理程序,但是当我向DFP广告管理系统的API提出请求时,代码首先失败了。 NAMESPACE and USE are declared at top of the php script that the jQuery is being sent to. NAMESPACEUSE在将jQuery发送到的php脚本的顶部声明。

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. 如果有可能,我需要哪些工具来确保jQuery请求尊重NAMESPACEUSE

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? 在这种情况下是否有偏好使用jQuery中的特定数据类型来更好地处理xml,json,脚本或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> 

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. 这是我用来将ajax请求定向到的路径。 Because I am using a WordPress framework the request needed to be made aa relative URL using path templates within WordPress. 因为我使用的是WordPress框架,所以需要使用WordPress中的路径模板将请求发送为相对URL。 So instead of using a absolute URL I used a WordPress based one and everything worked flawlessly. 因此,我没有使用绝对URL,而是使用了基于WordPress的URL,并且一切都正常运行。

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

Instead of 代替

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM