我创建了一个名为httpproviders的服务
import {Injectable} from '@angular/core';
import { Http, Response, Request,RequestMethod, Headers} from '@angular/http';
import 'rxjs/add/operator/map';
//import 'rxjs/Rx';
@Injectable()
export class Httpprovider {
cities: Response;
http: Http;
constructor(http: Http){
this.http = http;
}
httpReq(url: string, method: string, data: any, header: Headers){
let headers = new Headers();
headers.append('Content-Type', 'application/json');
//console.log(headers);
if (method === 'GET'){ var methods = RequestMethod.Get}
else if (method === 'POST'){ var methods = RequestMethod.Post}
else if (method === 'PUT'){var methods = RequestMethod.Put}
else if (method === 'PATCH'){var methods = RequestMethod.Patch}
else if (method === 'DELETE'){var methods = RequestMethod.Delete}
else {methods = RequestMethod.Get};
return this.http.request(new Request({ <<<<<<
method: methods, <<<<<<
url: url, <<<<<<
body: JSON.stringify(data), <<<<<<
headers: headers <<<<<<
})).map(res => res.json());
}
}
但是我的返回请求总是说我的参数不匹配, 当我做一个帖子请求它做得很好,但是当我得到请求它会发送这样的错误:
EXCEPTION: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at Response.Body.json (:3000/node_modules/@angular/http/bundles/http.umd.js:768)
at MapSubscriber.eval [as project] (:3000/app/service/httpprovider.js:47)
at MapSubscriber._next (:3000/node_modules/rxjs/operator/map.js:77)
at MapSubscriber.Subscriber.next (:3000/node_modules/rxjs/Subscriber.js:89)
at XMLHttpRequest.onLoad (:3000/node_modules/@angular/http/bundles/http.umd.js:1168)
at ZoneDelegate.invokeTask (zone.js:275)
at Object.onInvokeTask (:3000/node_modules/@angular/core/bundles/core.umd.js:4401)
at ZoneDelegate.invokeTask (zone.js:274)
at Zone.runTask (zone.js:151)
at XMLHttpRequest.ZoneTask.invoke (zone.js:345)
和我的包json
"@angular/http": "~2.4.0",
"rxjs": "5.0.1",
当我请求获取http
时,这是在我的组件中public concept: any[] = []; constructor( private _router: Router, private _httpprovider: Httpprovider) { } ngOnInit(){ this.getconcept(); } getconcept(){ this._httpprovider.httpReq('http://localhost:5000/getconcept','GET',null,null).subscribe((data)=>{ for (let i=0;i<data.length;i++){ this.concept.push(data[i]); } }); };
知道可能出现什么问题吗?
以下是我从服务器请求获取的内容
[{"cbncept_desc":"AKkery"},{"cbncept_desc":"CBFDee & Other Bevereges"},{"cbncept_desc":"Dessert"},{"cbncept_desc":"Fast Food"},{"cbncept_desc":"Food CBurt"},{"cbncept_desc":"Restaurant"}]