I can't access data from api.I am getting following error in get request.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Angular 2 Service Example</title>
<!-- libraries -->
<script src="https://rawgithub.com/systemjs/systemjs/0.19.6/dist/system.js"></script>
<script src="https://code.angularjs.org/tools/typescript.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.0/angular2-polyfills.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.0/Rx.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.0/angular2.dev.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.0/http.dev.js"></script>
<script>
System.config({
transpiler: 'typescript',
typescriptOptions: {
emitDecoratorMetadata: true
},
packages: {
'src': {
defaultExtension: 'ts'
}
}
});
</script>
<script>
System.import('src/boot');
</script>
</head>
<body>
<app-component>Loading...</app-component>
</body>
</html>
app.component.tpl.html
<form (ngSubmit)="getCountriesByRegion()">
<label>Region:</label>
<input type="text" [(ngModel)]="region" required>
<button type="submit">Search</button>
</form>
<hr>
<p style="color:red">{{error}}</p>
<h1>Countries in {{region}}</h1>
<pre>{{countries|json}}</pre>
<div *ngFor="#country of countries">{{country.name}}</div>
app.component.ts
import {Component} from 'angular2/core';
import {CountryService} from './service/country.service'
@Component({
selector: 'app-component',
templateUrl: 'src/app.component.tpl.html',
providers: [CountryService]
})
export class AppComponent {
constructor(private _countryService: CountryService){
this._countryService = _countryService;
}
getCountriesByRegion(){
this.error = "";
this.countries = [];
this._countryService.getCountriesByRegion(this.region)
.subscribe(
data => this.countries = data,
error => this.error = "Region " + this.region + " is invalid."
);
}
}
boot.ts
import {bootstrap} from 'angular2/platform/browser';
import {HTTP_PROVIDERS} from 'angular2/http';
import {AppComponent} from './app.component';
bootstrap(AppComponent, [HTTP_PROVIDERS]);
country.service.ts
import {Injectable} from "angular2/core"
import {Http} from 'angular2/http';
import 'rxjs/Rx'
@Injectable()
export class CountryService{
endpoint_url:String = "http://192.168.2.3:8080/erp/hello";
constructor(http: Http){
this.http = http;
}
getCountriesByRegion (region:String){
return this.http.get(this.endpoint_url + '?name=' + region).map(res => res.json());
}
}
I am getting following output.
Output
At serverside..
@RequestMapping(value = "/hello", method = RequestMethod.GET , produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String printHello(@RequestParam("name") String name) {
System.out.println("method is called.................... : " + name);
String test = "hello world" ;
return test;
}
I am use java in back end for API provider. Thanks in advance....