2

I'm using Angular 2.RC4 and typescript

I have class with data:

    import {Injectable, Injector, ReflectiveInjector} from '@angular/core';
import {
    Http, Response, Headers, RequestOptions, HTTP_PROVIDERS, Jsonp, JSONP_PROVIDERS,
    URLSearchParams
} from '@angular/http';
import { Observable } from 'rxjs/Observable';
import '../helpers/rxjs-operators';
import { caaConfig } from "../config/cmsaa";

getData(url: string): Observable<any>{
        let params  = new URLSearchParams();
        params.set('callback', 'JSONP_CALLBACK');
        url = this.domain + url;
        let cacheKey = 'cache_caa_http_get_' + url;
        if(this._cache[cacheKey]){
            return this._cache[cacheKey];
        }else{
            let request = this.jsonp.get(url, {search:params}).map(this.extractData).catch(this.handleError);
            this._cache[cacheKey] = request;
            return request;
            }
    }

In all sources: github issues, stackoverflow posts I saw that add callback parameter solve problem. In my case not.

My error is:

JSONP injected script did not invoke callback.

and url

http://example.com/project/invoices/4137?callback=ng_jsonp.__req0.finished

In my bootstrap in included JSON_PROVIDERS.

Someone have idea what I should fix to get data from my remote server?

8
  • 1
    And what is the response you get to the HTTP request? (I assume that you aren't actually running your service on domain.com (please use example.com for example domains, that is what it is there for, domain.com is a real website.))
    – Quentin
    Commented Aug 10, 2016 at 7:07
  • When I call in browser answer is JSON. By angular response is: ``` body: JSONP injected script did not invoke callback. status: 200 ok: true, type: 3 url: example.com/project/invoices/4137?callback=__ng_jsonp_.__req0.finished ``` this is error received because I have as error handler return Observable.throw(error); I see this error in console, because in "network" tab in browser this request doesn't exists
    – jaroApp
    Commented Aug 10, 2016 at 7:33
  • That's the problem then. You are getting JSON instead of JSONP. If you are using JSONP then you have to get JSONP from the server as well as handling JSONP on the client.
    – Quentin
    Commented Aug 10, 2016 at 7:33
  • to get JSON should I use JSON_CALLBACK instead JSONP_CALLBACK? I've been tried but still I have same error. I have not editable API on remote server which return JSON
    – jaroApp
    Commented Aug 10, 2016 at 7:42
  • If the remote server is returning JSON then you can't use JSONP. There is no JSON_CALLBACK option, the callback in the URL is a feature specific to JSONP.
    – Quentin
    Commented Aug 10, 2016 at 7:43

1 Answer 1

0

Your code assumes that your server application uses the callback parameter in the returned payload. Your server could use another parameter like c.

The content of this parameter is used to generated a response payload like below:

__ng_jsonp__.__req0.finished({ ... })

Otherwise, the callback won't be called within Angular 2 (what you have in your case).

Not the answer you're looking for? Browse other questions tagged or ask your own question.