【问题标题】:Angular2 - The requested url was not found on this serverAngular2 - 在此服务器上找不到请求的 url
【发布时间】:2017-09-14 07:01:59
【问题描述】:

我有一个 angular2 应用程序,它击中了一个 api,它位于我的 wamp/www 中。当我尝试点击 api 时出现此错误。

404 未找到

未找到

在此服务器上找不到请求的 URL /usmanproject/api/web/v1/users。


Apache/2.4.9 (Win64) PHP/5.5.12 服务器在 localhost 端口 80

我的 Angular 应用程序正在 localhost:4200 上运行,并且 Angular 项目文件夹也放置在 wamp/www 中,但它无法正常工作。

这是我的角码

 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 中制作的。我已经在邮递员中测试了apihttp://localhost/usmanproject/api/web/v1/users?access-token=n-EJtZiejtz5RSVWe-U14G4kCnPWMKf0,它在那里工作得非常好。

它在 google chrome 中运行良好,但在 firefox 上却不行。

【问题讨论】:

  • 你的 api 是否在 80 端口上运行?
  • 不,不是。它只是像这样简单地运行 localhost/usmanproject/api/web/v1/users?access-token=n-EJtZiejtz5RSVWe-U14G4kCnPWMKf0
  • 您没有告诉我们有关您的后端的任何信息,因此只能猜测并提出其他问题。你应该至少包含一些关于你验证后端真的在你写的 URL 上运行的信息。
  • 让我写吧
  • 你是否在 web Api 中启用了“cors”

标签: angular api http localhost port


【解决方案1】:

您需要检查将项目重定向到的虚拟主机。 在 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]

【讨论】:

    猜你喜欢
    • 2017-03-21
    • 2010-10-16
    • 1970-01-01
    • 2017-12-26
    • 2015-03-30
    • 2014-12-13
    • 2016-10-31
    • 2017-04-19
    • 2016-11-23
    相关资源
    最近更新 更多