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

标签 angular api http localhost port

我有一个 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,它运行良好那边很好。

enter image description here

它在 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/

相关文章:

angular - 将值传递给子组件 Angular2

javascript - Google API 不会翻译外文字母。

java - Long.reverse(long) 方法有什么用?

http - HTTP 响应 header 中的校验和 - 为什么不呢?

http - 有什么方法可以自定义 HTTP 响应状态?

使用空主体获取 grails Controller 操作时出现 Json 解析错误

get - 带有正文的 Angular2 http GET?

angular - 自定义组件值未使用 Angular Reactive Form 进行更新

javascript - 如何在 typescript 中模拟 XMLHTTPRequest 以进行单元测试

node.js - 如何使用VueJs和Axios显示表中的所有用户?