问题描述
我正在使用 angular-cli
安装的 angular 4.0.0
来创建一个简单的应用程序,但我一直收到 404 File not found代码>尝试调用 PHP 文件时出错.
I'm using angular 4.0.0
instaled with angular-cli
to create a simple application, but I keep getting a 404 File not found
error when trying to call a PHP file.
基本上,我使用 ng serve
启动 Angular,它在 http://localhost:4000
上运行应用程序,并且我还运行 WAMP,所以我可以使用 php 文件连接到 phpmyadmin 并从后端数据库获取数据.
Basically I'm starting the Angular using ng serve
, which runs the application on http://localhost:4000
and I also have WAMP running, so I can use the php file to connect to a phpmyadmin and get data from a backend database.
我曾经用 Angular 1.5+ 做这件事,它工作得很好,但现在我不断收到这个错误:
I used to do it with Angular 1.5+ and it worked fine, but now I keep getting this error:
http://localhost:4000/api/server 404(未找到)
这是我的文件夹结构:
root
- src
--api
--app
--assets
// etc..
如果我尝试调用其他网址,例如,在实际服务器上在线的网址,那么它会按预期工作.
If I try to call other url, for example, one that is online on an actual server, then it works as expected.
这是负责 http 调用的文件:
This is the file responsable for the http calls:
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Rx';
@Injectable()
export class ApiService {
constructor(
private http: Http,
) { }
apiGet() {
return this.http.get(
'./api/server'
).map(response => response.json()).do(data => {
// -
}).catch(this.handleErrors);
}
apiPost(info: object) {
const headers = new Headers();
headers.append('Access-Control-Allow-Origin', '*');
return this.http.post(
'./api/server',
JSON.stringify(info),
{ headers }
).map(response => response.json()).do(data => {
console.log( 'Response: ', data )
// -
}).catch(this.handleErrors);
}
handleErrors(error: Response) {
console.log( 'Error: ', error )
return Observable.throw(error);
}
}
我在这里做错了什么?
推荐答案
错误,它自己解释了整个事情
Error it self explains the whole things
http://localhost:4000/api/server
Angular 正在尝试从 localhost:4000
调用 api,但它应该只是 localhost
.
Angular is trying to call api from localhost:4000
but it should be only localhost
.
请检查您的请求 url 并将其设为 http://localhost/api/server 而不是 http://localhost:4000/api/server
Please check your request url and make it http://localhost/api/server not http://localhost:4000/api/server
这篇关于使用Angular4调用php文件并且找不到cli返回页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!