Angular2 - 在此服务器上找不到请求的URL

时间:2017-09-14 07:01:59

标签: angular api http localhost port

我有一个angular2应用,它点击api,放在我的wamp/www中。当我尝试点击api时,我得到了这个错误。

  

             404未找到              

未找到

      

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

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

我的角度应用程序在localhost:4200上运行,角度项目文件夹也放在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。我在邮递员中测试了api http://localhost/usmanproject/api/web/v1/users?access-token=n-EJtZiejtz5RSVWe-U14G4kCnPWMKf0,它在那里工作得非常好。

enter image description here

它在谷歌浏览器中运行良好但在Firefox上没有。

1 个答案:

答案 0 :(得分:0)

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