我有一个代理在那里我想proxify AJAX
请求我发现,code和它的作品很好的:
I have a proxy where i want to proxify ajax
requests i have found that code and it works good:
(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
但我用 XMLHtt prequest
太...
But the problem is that i also have some ajax
request which i dont need to proxyify
but i use XMLHttpRequest
too...
和问题是我怎么可以过滤我的 AJAX
的要求,而忽略他们的的JavaScript
?
And the question is how can i filter my ajax
requests and ignore them in that javascript
?
添加一个公共接口来访问代理打开
电话:
Add a public interface to access the proxied open
call:
var proxied = window.XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open_noproxy = proxied;
然后就可以调用 XMLHtt prequest :: open_noproxy()
当你想使一个普通的AJAX调用。
Then you can call XMLHttpRequest::open_noproxy()
when you want to make a normal AJAX call.
另一种方法是把一个关键词在URL,如 NOPROXY:HTTP://无论
。您更换打开
功能可以检查是否URL以 NOPROXY:
;如果是的话,它会删除其从URL,然后跳过了code,在调用之前增加了代理preFIX 代理
。
Another way would be to put a keyword in the URL, e.g. NOPROXY:http://whatever
. Your replacement open
function can check whether the URL begins with NOPROXY:
; if so, it removes it from the URL and then skips over the code that adds the proxy prefix before calling proxied
.