[英]Angular on Rails API: HTTP Request
I'm doing a tutorial and I'm supposed to get a list of books to show up, but I get a console error saying " http://localhost:4200/api/books.json 404 (Not Found)". 我正在做一个教程,应该获取要显示的书籍列表,但是出现控制台错误,提示“ http:// localhost:4200 / api / books.json 404(未找到)”。
If I visit localhost:3000/api/books.json in my browser, I can see the seeded info. 如果在浏览器中访问localhost:3000 / api / books.json,则可以看到种子信息。 Should my app be trying for localhost:3000 or is it supposed to be using localhost:4200?
我的应用程序应该尝试使用localhost:3000还是应该使用localhost:4200?
app/client/src/app/book-list/book-list-component.ts app / client / src / app / book-list / book-list-component.ts
import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http';
@Component({
selector: 'app-book-list',
templateUrl: './book-list.component.html',
styleUrls: ['./book-list.component.css']
})
export class BookListComponent implements OnInit {
books: any;
constructor(private http: Http) { }
ngOnInit() {
this.http.get('/api/books.json')
.subscribe(response => this.books = response.json());
}
}
app/client/src/app/book-list/book-list-component.html app / client / src / app / book-list / book-list-component.html
<p>
book-list works!
</p>
<ul>
<li *ngFor="let book of books">{{ book.name }}</li>
</ul>
Here is the rest of the code base in case you need to look at the setup: https://github.com/theresaluu/tut-home-library-spike 如果需要查看设置,这是其余的代码库: https : //github.com/theresaluu/tut-home-library-spike
You should specify the whole URL if its on another domain, but also you need to have CORS enabled on the server (more on CORS here https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS ). 如果整个URL在另一个域上,则应指定整个URL,但是还需要在服务器上启用CORS(有关CORS的更多信息,请参见https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS ) 。 Angular is probably on another server (I assume you start it with ng serve), and that server is hosting the static files + URL rewrite (if you are using the HTML5 angular strategy) and the REST server that you are consuming is on a different port.
Angular可能在另一台服务器上(我假设您以ng serve来启动它),并且该服务器托管静态文件+ URL重写(如果您使用HTML5角度策略),而您使用的REST服务器在另一台服务器上港口。
It should be this.http.get('http://localhost:3000/api/books.json')
应该是
this.http.get('http://localhost:3000/api/books.json')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.