RichieRock
RichieRock

Reputation: 1168

How to avoid timeout error on jsonp request?

How to handle timeout of the jsonp request? I have a 10s timeout on the observable returned by jsonp.get() method but the request remains in browser even though timeout has reached. After 1-2 minute timeout I receive this error in console:

GET https://myserver/?url=asdf&callback=ng_jsonp.__req0.finished net::ERR_CONNECTION_TIMED_OUT browser_jsonp.js:64

In my component want to make a jsonp request and subscribe to it like this:

this.service.jsonp(url).subscribe(
    (response) => { this.handleResponse(response); },
    (err) => { this.handleError(err); }
  );

This is the jsonp method in my service:

jsonp(url: string): Observable<any> {
  let params = new URLSearchParams();

  params.set('url', url);
  params.set('callback', 'JSONP_CALLBACK');

  return this.jsonp.get('https://myserver', {search: params})
    .timeout(10000)
    .catch((err: Response) => Observable.throw(err))
    .map((response: Response) => response.json());
}

Upvotes: 1

Views: 1593

Answers (1)

kemsky
kemsky

Reputation: 15271

JSONP is actually adding <script> tag to the page. Angular just removes node from document (this is ok for IE and Edge, but in Firefox you will have to use document.onbeforescriptexecute, don't know how Chrome handles this), but script is still loading and eventually it dies with browser timeout and logs to console.

Upvotes: 1

Related Questions