【问题标题】:Filter ajax request by proxy通过代理过滤ajax请求
【发布时间】:2014-05-30 23:01:40
【问题描述】:

我有一个代理,我想代理 ajax 请求,我找到了该代码并且效果很好:

(function() {

        if (window.XMLHttpRequest) {

          function parseURI(url) {
            var m = String(url).replace(/^\s+|\s+$/g, "").match(/^([^:\/?#]+:)?(\/\/(?:[^:@]*(?::[^:@]*)?@)?(([^:\/?#]*)(?::(\d*))?))?([^?#]*)(\?[^#]*)?(#[\s\S]*)?/);
            // authority = "//" + user + ":" + pass "@" + hostname + ":" port
            return (m ? {
              href : m[0] || "",
              protocol : m[1] || "",
              authority: m[2] || "",
              host : m[3] || "",
              hostname : m[4] || "",
              port : m[5] || "",
              pathname : m[6] || "",
              search : m[7] || "",
              hash : m[8] || ""
            } : null);
          }

          function rel2abs(base, href) { // RFC 3986

            function removeDotSegments(input) {
              var output = [];
              input.replace(/^(\.\.?(\/|$))+/, "")
                .replace(/\/(\.(\/|$))+/g, "/")
                .replace(/\/\.\.$/, "/../")
                .replace(/\/?[^\/]*/g, function (p) {
                  if (p === "/..") {
                    output.pop();
                  } else {
                    output.push(p);
                  }
                });
              return output.join("").replace(/^\//, input.charAt(0) === "/" ? "/" : "");
            }

            href = parseURI(href || "");
            base = parseURI(base || "");

            return !href || !base ? null : (href.protocol || base.protocol) +
            (href.protocol || href.authority ? href.authority : base.authority) +
            removeDotSegments(href.protocol || href.authority || href.pathname.charAt(0) === "/" ? href.pathname : (href.pathname ? ((base.authority && !base.pathname ? "/" : "") + base.pathname.slice(0, base.pathname.lastIndexOf("/") + 1) + href.pathname) : base.pathname)) +
            (href.protocol || href.authority || href.pathname ? href.search : (href.search || base.search)) +
            href.hash;

          }

          var proxied = window.XMLHttpRequest.prototype.open;
          window.XMLHttpRequest.prototype.open = function() {
              if (arguments[1] !== null && arguments[1] !== undefined) {
                var url = arguments[1];
                url = rel2abs("' . $url . '", url);
                url = "' . PROXY_PREFIX . '" + url;
                arguments[1] = url;
              }
              return proxied.apply(this, [].slice.call(arguments));
          };

        }

      })();

但问题是我也有一些ajax 请求,我不需要proxyify,但我也使用XMLHttpRequest...

问题是我如何过滤我的ajax 请求并在javascript 中忽略它们?

【问题讨论】:

    标签: javascript php ajax proxy


    【解决方案1】:

    添加一个公共接口来访问代理的open调用:

    var proxied = window.XMLHttpRequest.prototype.open;
    window.XMLHttpRequest.prototype.open_noproxy = proxied;
    

    那么当你想进行正常的AJAX调用时,你可以调用XMLHttpRequest::open_noproxy()

    另一种方法是在 URL 中添加关键字,例如NOPROXY:http://whatever。你的替换open函数可以检查URL是否以NOPROXY:开头;如果是这样,它会从 URL 中删除它,然后在调用 proxied 之前跳过添加代理前缀的代码。

    【讨论】:

    • 非常感谢,但我找到了另一种方法,如果我知道我的url,我正在尝试ajax 我通过arguments[1] != "http://mysite.co/ajax.php" 检查它们,无论如何我认为你的方法更好。跨度>
    • 我以为您正在寻找比那个明显的解决方案更通用的东西。
    猜你喜欢
    • 2019-03-06
    • 1970-01-01
    • 2022-08-07
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 2023-03-18
    • 2013-04-12
    • 1970-01-01
    相关资源
    最近更新 更多