带有清漆缓存的 AJAX GET 请求 [英] AJAX GET requests with Varnish Cache

查看:36
本文介绍了带有清漆缓存的 AJAX GET 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在带有清漆的服务器上运行了一个 AJAX 请求.请求如下:

I have got an AJAX request running on a server with varnish. The request is as follows:

    (function() {
        $("#name").autocomplete({
            minLength:3, //minimum length of characters for type ahead to begin
            source: function (request, response) {
                $.ajax({
                    type: 'GET',
                    url: php_vars.var_1, //your server side script
                    dataType: 'json',
                    data: {
                        postcode: request.term
                    },

                    success: function (data) {
                        alert("Success");
                    }
                });
            }
        });
})();

对于 url,我使用 wp_localize_scripts 和带有 php 脚本绝对 URL 的数组.我已经在这个脚本之前安慰过这个网址,没关系.

For the url, I use wp_localize_scripts and array with the absolute URL of the php script. I have consoled this url before this script and it is ok.

我在未使用清漆运行的服务器上进行了此设置,并且运行良好.但是在我的带有清漆的服务器上,我注意到请求 URL 不正确(应该是auspost.php",而是带有查询参数的页面 url).在我的无清漆服务器上,GET 请求 url 是正确的.

I have this setup on a server not running with varnish and it works fine. However on my server with varnish, I have noticed that the request URL is not correct (should be "auspost.php" and instead it is the page url with the query params). On my none varnish server the GET request url is correct.

看起来 varnish 正在缓存我的 GET 请求.任何建议将不胜感激!如果需要,我可以粘贴我的 vcl 配置吗?

It looks like varnish is caching my GET requests. Any advice would be very much appreciated! I can pastebin my vcl config if need be?

推荐答案

对于 ajax 请求,你应该有以下标题可用X-Reqeusted-With: XMLHttpRequest

For ajax requests you should have the following header available X-Reqeusted-With: XMLHttpRequest

在您的清漆 vcl_recv 中,您可以检查此标头是否存在并强制通过.

In your varnish vcl_recv you can check if this header is present and force a pass.

if (req.http.X-Requested-With == "XMLHttpRequest"){
return (pass);
}

这篇关于带有清漆缓存的 AJAX GET 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆