In angular2 needed to create a global intercept http request, to capture including page requests via templateUrl
in @Component
decorator.
Then I implemented as follows:
(function (open) {
XMLHttpRequest.prototype.open = function (method, url, async, user, pass) {
this.addEventListener("readystatechange", function (data) {
if (data.target.readyState === 4) {
console.log(data);
console.log(data.target.responseURL);
}
}, false);
open.call(this, method, url, async, user, pass);
};
})(XMLHttpRequest.prototype.open);
But this implementation works but ends up causing a recurring error on the console, when making a request to the server via object Http
, despite the implementation seem to continue functioning properly.
The error is the following:
core.umd.js:3370 EXCEPTION: More tasks executed then were scheduled.
core.umd.js:3375 ORIGINAL STACKTRACE:
core.umd.js:3376 Error: More tasks executed then were scheduled.
at ZoneDelegate._updateTaskCount (zone.js:269)
at ZoneDelegate.invokeTask (zone.js:240)
at Zone.runTask (zone.js:136)
at ZoneTask.invoke (zone.js:304)
at XMLHttpRequest.<anonymous> (zone.js:1266)
at ZoneDelegate.invokeTask (zone.js:236)
at Object.onInvokeTask (core.umd.js:6149)
at ZoneDelegate.invokeTask (zone.js:235)
at Zone.runTask (zone.js:136)
at XMLHttpRequest.ZoneTask.invoke (zone.js:304)
And before the error appears the following warnnig:
Synchronous XMLHttpRequest on the main thread is deprecated
because of its detrimental effects to the end user's experience.
For more help, check https://xhr.spec.whatwg.org/.
I saw the zone.js abstracts and manages asynchronous requests and I believe that the line open.call(this, method, url, async, user, pass);
is the cause of the problem. So I think I should make this code block also managed zone.js, since it looks like I'm making the request out of it with this interceptor. That's right? How would I do that?
Environment:
- Angular: 2.0.2;
- zone.js: 0.6.25