Angular 4: JSONP injected script did not call callback
I'm new to Anguar4 and came across this question:
JSONP script injected did not call callback
I tried different APIs, for example: https://jsonplaceholder.typicode.com/posts
However my api is giving me this error. However, it works for jQuery, jsonp. I googled a lot of resources online, spent many hours but couldn't fix it. Here is my code:
import { Injectable } from '@angular/core';
import { Http, Headers, Jsonp, URLSearchParams } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class ServerService{
constructor(private jsonp: Jsonp){}
getServers(term: string) {
let url = `url`;
let params = new URLSearchParams();
params.set('search', term); // the user search value
params.set('action', 'opensearch');
params.set('format', 'json');
params.set('callback', 'JSONP_CALLBACK');
return this.jsonp
.get(url, { search: params })
.subscribe(
(data) => {
console.log(data);
},
(error) => {
console.log(error);
});
}
}
+3
source to share
2 answers
Use __ng_jsonp__.__req0.finished
instead JSONP_CALLBACK
!
import { Injectable } from '@angular/core';
import { Http, Headers, Jsonp, URLSearchParams } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class MyServerService{
constructor(private jsonp: Jsonp){}
getServers(term: string) {
let url = `https://jsonplaceholder.typicode.com/posts`;
let params = new URLSearchParams();
params.set('search', term); // the user search value
params.set('action', 'opensearch');
params.set('format', 'json');
params.set('callback', '__ng_jsonp__.__req0.finished');
return this.jsonp
.get(url, { search: params })
.subscribe(
(data) => {
console.log(data);
},
(error) => {
console.log(error);
});
}
}
+3
source to share