我有一个 angular2 应用程序,它命中了一个 api
,它被放置在我的 wamp/www
中。当我尝试访问 api 时出现此错误。
404 Not Found
Not Found
The requested URL /usmanproject/api/web/v1/users was not found on this server.
Apache/2.4.9 (Win64) PHP/5.5.12 Server at localhost Port 80
我的 Angular 应用程序在 localhost:4200
上运行,并且 Angular 项目文件夹也放在 wamp/www
中,但它不工作。
这是我的 Angular 代码
private heroesUrl = 'http://localhost/usmanproject/api/web/v1/users?access-token=n-EJtZiejtz5RSVWe-U14G4kCnPWMKf0'; // URL to web api
private headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
create(): Promise<any> {
return this.http
.get(this.heroesUrl)
.toPromise()
.then(res => res.json().data)
.catch(this.handleError);
}
后端是在 yii2
中制作的。我已经在 postman 中测试了 api
http://localhost/usmanproject/api/web/v1/users?access-token=n-EJtZiejtz5RSVWe-U14G4kCnPWMKf0
,它运行良好那边很好。
它在 google chrome 中运行良好,但在 firefox 上运行不正常。
最佳答案
您需要检查将项目重定向到的虚拟主机。 在 Angular 上它将重定向到 dist/projectName/ 并且你应该添加一个 .htaccess 应该看起来像这样:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC,L]
关于Angular2 - 在此服务器上找不到请求的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46212554/