先说问题结论,httpclient的get返回的对象的subscribe方法中会将字符创解析成json,如果直接返回一般的字符串可能会提示JSON.parse错误
引用http对应的类型
import { HttpClient, HttpParams } from '@angular/common/http';
在自定义的httpservice类constructor中注入
constructor(public http: HttpClient)
设置get方法的options参数reqOpts
options参数如下
options: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
context?: HttpContext;
observe?: 'body';
params?: HttpParams | {
[param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>;
};
reportProgress?: boolean;
responseType: 'arraybuffer';
withCredentials?: boolean;
transferCache?: {
includeHeaders?: string[];
} | boolean;
}
然后调用且返回
return this.http.get(this.url + '/' + endpoint+params, reqOpts);
在后台webapi中直接返回字符串,在ionic中会提示错误
error
: {error: SyntaxError: Unexpected token '/', "/* * This "... is not valid JSON at JSON.parse (<anonymous>…