angular - Angular2 http get restfull api request not working -
i got angular2 , ran problem don't see how fix. trying data restfull api , worked fine. problem when tried restfull api, test, not me data.
so call api worked: http://jsonplaceholder.typicode.com/posts
works() { return new promise(resolve => { this.http.get('http://jsonplaceholder.typicode.com/posts') .map(res => res.json()) .subscribe(data => { this.data = data; console.log(data); resolve(this.data); }, error => console.error(error)); }); }
but call api did not: http://gokhankaradas.com/wp-json/posts goes error condition. (object {_body=event error, status=200, statustext="ok", ...})
doesnotwork() { return new promise(resolve => { this.http.get('http://gokhankaradas.com/wp-json/posts') .map(res => res.json()) .subscribe(data => { this.data = data; console.log(data); resolve(this.data); }, error => console.error(error)); }); }
am missing something?
i might have solved myself.
i created simple result via laravel, product list, , spit out in json format , tested it. failed failing api.
public function test(product $product){ $products = $product->get(); return response::json($products, 200); }
then added following code routes.php , worked. guess api failing didnt have that. not sure bout seems fix problem ;)
header('access-control-allow-origin: *'); header('access-control-allow-methods: post, get, options, put, delete');
Comments
Post a Comment