我正在尝试连接PHP后端(MySQL数据库)和AngularJS前端。我花了大约两天的时间来阅读教程,这让我很沮丧,也花了很多时间。
基本上我使用的是
$http.get('/angular/index.php').success(function (data) {
console.log(data);
});
这就提出了以下要求:
GET http://localhost:9000/angular/index.php 404 (Not Found)
问题是该服务只能从以下位置访问:
angular/index.php
如何更改/使用Angular向本地数据存储/数据库发送请求??
RESTful设置
虚拟主机:
<VirtualHost *:9000>
ServerAdmin webadmin
DocumentRoot "/Users/biggles/Sites/angular/app/ajax"
ServerName angular
ServerAlias angular
ErrorLog "/private/var/log/apache2/angular-error_log"
CustomLog "/private/var/log/apache2/angular-access_log" common
</VirtualHost>
主机文件:
127.0.0.1 angular
要从angular应用程序访问http://angular/index.php
,只需使用以下代码段angular
是您的主机名,而不是路径的一部分
$http.get('/index.php').success(function (data) {
console.log(data);
});
Angular$http服务的行为就像通常的链接一样:
如果您的url以方案名称开头,例如http://
或//
$http将尝试执行外部请求。然而,这需要CORS配置或JSONP。
如果您的url以斜杠开头,/
$http将使用当前方案名称、用户信息、主机名和端口,但将替换完整路径。
如果你的url既没有方案名称也没有斜杠,那么你的路径将是相对的。
来自维基百科:
foo://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose
'_/ '_______________/ '_________/ '__/ '______________________/ '__/
| | | | | |
| userinfo hostname port query fragment
| '________________________________/'___________________/
scheme | |
name authority |
path
'_______________________________________/
|
host