angular 2 服务未调用 php 文件
angular 2 service not calling a php file
我正在学习 angular 2 并且我进行了连接到数据库的测试,但我得到一个错误:语法错误:意外标记 < in JSON at position 0.
所以我的 xampp 在端口 80 或 443 上工作。
我测试的 url(我正在使用 angular cli)是:localhost:4200
这是我的尝试。我有这种方法的服务:
getUsers(){
return this._http.get('getusers.php')
.map(res => res.json());
}
getusers.php 文件位于 public 文件夹中。
这是它的内容。非常基本,只是为了获得一些结果并将它们作为 json:
发回
// set up the connection variables
$db_name = 'mydb';
$hostname = 'localhost';
$username = 'myusername';
$password = 'mypass';
// connect to the database
$dbh = new PDO("mysql:host=$hostname;dbname=$db_name", $username, $password);
$sql = 'SELECT * FROM users';
$stmt = $dbh->prepare( $sql );
$stmt->execute();
$result = $stmt->fetchAll( PDO::FETCH_ASSOC );
$json = json_encode( $result );
echo $json;
所以我在导入服务的组件中添加,在构造函数中初始化它并订阅:
constructor(private _usersService: UsersService) {}
ngOnInit() {
this._usersService.getUsers()
.subscribe(
data => this.getUsers = JSON.stringify(data),
error => alert(error),
() => console.log("Finihed")
);
}
我在这里错过了什么。我打了很多 ajax 电话,但第一次打 angular。也许是港口?还是我缺少的其他东西?
此致
您需要订阅 ajax 工作电话;
调用 getUsers 方法的人应该订阅它:
getUsers(){
return this._http.get('getusers.php')
.map(res => res.json());
}
假设您在这里调用 get 方法:
myOtherFunction(){
this.getUsers.subscribe((response)=>{
console.log('response',response);
})
}
所以既然是跨域的情况就是两个端口。在您的通话中使用完整的 url 端口:
http://localhost:80/yourproject/public/getContacts.php
并在您的服务器端添加 CORS headers 在输出之前:
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Headers: X-Requested-With');
header('Content-Type: application/json');
echo json_encode($result);
因为我只是在测试,所以我使用了 *,但不推荐这样做。通常允许特定的 url.
我正在学习 angular 2 并且我进行了连接到数据库的测试,但我得到一个错误:语法错误:意外标记 < in JSON at position 0.
所以我的 xampp 在端口 80 或 443 上工作。 我测试的 url(我正在使用 angular cli)是:localhost:4200
这是我的尝试。我有这种方法的服务:
getUsers(){
return this._http.get('getusers.php')
.map(res => res.json());
}
getusers.php 文件位于 public 文件夹中。 这是它的内容。非常基本,只是为了获得一些结果并将它们作为 json:
发回 // set up the connection variables
$db_name = 'mydb';
$hostname = 'localhost';
$username = 'myusername';
$password = 'mypass';
// connect to the database
$dbh = new PDO("mysql:host=$hostname;dbname=$db_name", $username, $password);
$sql = 'SELECT * FROM users';
$stmt = $dbh->prepare( $sql );
$stmt->execute();
$result = $stmt->fetchAll( PDO::FETCH_ASSOC );
$json = json_encode( $result );
echo $json;
所以我在导入服务的组件中添加,在构造函数中初始化它并订阅:
constructor(private _usersService: UsersService) {}
ngOnInit() {
this._usersService.getUsers()
.subscribe(
data => this.getUsers = JSON.stringify(data),
error => alert(error),
() => console.log("Finihed")
);
}
我在这里错过了什么。我打了很多 ajax 电话,但第一次打 angular。也许是港口?还是我缺少的其他东西?
此致
您需要订阅 ajax 工作电话; 调用 getUsers 方法的人应该订阅它:
getUsers(){
return this._http.get('getusers.php')
.map(res => res.json());
}
假设您在这里调用 get 方法:
myOtherFunction(){
this.getUsers.subscribe((response)=>{
console.log('response',response);
})
}
所以既然是跨域的情况就是两个端口。在您的通话中使用完整的 url 端口: http://localhost:80/yourproject/public/getContacts.php
并在您的服务器端添加 CORS headers 在输出之前:
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Headers: X-Requested-With');
header('Content-Type: application/json');
echo json_encode($result);
因为我只是在测试,所以我使用了 *,但不推荐这样做。通常允许特定的 url.