对于jsonp请求,我收到此错误:
Error: JSONP injected script did not invoke callback.
不确定如何使用回调。
getSellers(): Observable<Seller[]> {
return this.http.jsonp<Seller[]>(this.apiRoot + '/sellers', 'callback')
.pipe(
tap(_ => console.log('fetched sellers')),
catchError(this.handleError('getSellers', []))
);
}
错误:
HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "JSONP Error", url: "http://localhost:4300/api/sellers?__ng_jsonp__.__req$%7Bthis.times%7D.finished=ng_jsonp_callback_0", ok: false, …}
error: Error: JSONP injected script did not invoke callback. at HTMLScriptElement.onLoad (http://localhost:4200/vendor.js:7224:32) at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:2743:31) at Object.onInvokeTask (http://localhost:4200/vendor.js:36916:33) at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:2742:36) at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (http://localhost:4200/polyfills.js:2510:47) at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (http://localhost:4200/polyfills.js:2818:34) at invokeTask (http://localhost:4200/polyfills.js:3862:14) at HTMLScriptElement.globalZoneAwareCallback (http://localhost:4200/polyfills.js:3888:17)
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, headers: Map(0)}
message: "Http failure response for http://localhost:4300/api/sellers?__ng_jsonp__.__req$%7Bthis.times%7D.finished=ng_jsonp_callback_0: 0 JSONP Error"
name: "HttpErrorResponse"
ok: false
status: 0
statusText: "JSONP Error"
url: "http://localhost:4300/api/sellers?__ng_jsonp__.__req$%7Bthis.times%7D.finished=ng_jsonp_callback_0"
答案 0 :(得分:0)
尝试一下
getSellers(): Observable<Seller[]> {
return this.http.get(this.apiRoot + '/sellers')
.pipe(
tap(_ => console.log('fetched sellers')),
catchError(this.handleError('getSellers', []))
);
}
答案 1 :(得分:0)
我只是重新格式化答案
getSellers(): Observable<Seller[]> {
return this.http.jsonp(this.apiRoot + '/sellers', "callback")
.pipe(
map(res => {
reutrn res.results.map(item => {
return new Seller(
item.name,
item.id
);
});
})
tap(_ => console.log('fetched sellers')),
catchError(this.handleError('getSellers', []))
);