how to get data from WebMethod in angular2
Your method is just returning Observable
, they will just sit as a function. Observable
are lazy in nature. They wouldn't get executed until you subscribe to them.
AttendanceMast() { return this._http.get("AttendanceMast.apsx/GetDistinctBatch") .map((response) => response.toString()); //better do it response.json();}myService.AttendanceMast().subscribe( data => console.log(data);)
dataservice
getwithParamter(): Observable<JSON> { this.headers = new Headers(); this.headers.append('Content-Type', 'application/json; charset=utf-8'); let options = new RequestOptions({ method: RequestMethod.Post, url: "AttendanceMast.aspx/HelloWorldss", headers: this.headers, body:{program: 'pravin'} //same like data: "{}" in ajax }); return this._http.request(new Request(options)).retry(2) .map((response: Response) => response.text()) .do(data => console.log('All: ' + JSON.stringify(data))) .catch(this.handleError); } handleError(error: any) { console.error('An error occurred', error); return Promise.reject(error.message || error);}
C#
[System.Web.Services.WebMethod]public static string HelloWorldss(string program){ return "welcome Angular" + program;}
call
this.dataService.getwithParamter().subscribe( tradeshows => this.getwithparamter = tradeshows, error => console.error('Error: ' +error) );
print this variable showing your output this.getwithparamter
I assume you are using the standard configuration for a WebService. If so, I believe your problem is that your are using the inbuilt rest capability of Angular2 to access a WebService, aka a Soap service, returning xml soap envelopes. Hence the 404.
Unfortunately, you have a bit more work to do to make it work than if you were accessing a rest service.Have a look at angular2-soap on github. It should get you on the right track.