无法使用我的登录项目获取php后端的数据
我试图创建一个角度项目,但当我试图从php文件中获取数据时,它不允许我这样做,并且出现了一个错误,例如 if(data.success)上的“对象”类型上不存在属性“success”{ window.alert(data.message)上的类型“Object.”上不存在属性“message” 这是密码 这是我的登录组件:无法使用我的登录项目获取php后端的数据,php,json,angular,Php,Json,Angular,我试图创建一个角度项目,但当我试图从php文件中获取数据时,它不允许我这样做,并且出现了一个错误,例如 if(data.success)上的“对象”类型上不存在属性“success”{ window.alert(data.message)上的类型“Object.”上不存在属性“message” 这是密码 这是我的登录组件: import { Component, OnInit } from '@angular/core'; import { AuthService } from '../auth
import { Component, OnInit } from '@angular/core';
import { AuthService } from '../auth.service';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
constructor(private Auth: AuthService) { }
ngOnInit() {
}
loginUser(event){
event.preventDefault()
const target = event.target
const username = target.querySelector('#username').value
const password = target.querySelector('#password').value
this.Auth.getUserDetails(username, password).subscribe(data => {
if(data.success){ //this part is the error
} else {
window.alert(data.message) //this part too
}
})
}
}
这是我的授权服务:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'
@Injectable({
providedIn: 'root'
})
export class AuthService {
constructor(private http: HttpClient) { }
getUserDetails(username, password){
return this.http.post('/api/auth.php', {
username,
password
})
}
}
这是我的php文件auth.php:
<?php
$_POST = json_decode(file_get_contents('php://input'), true);
if(isset($_POST) && !empty($_POST)){
$username = $_POST['username'];
$password = $_POST['password'];
if($username == 'admin' && $password == 'admin'){
?>
{
"success": true,
"secret": "This is the secret no one knows but the admin"
}
<?php
} else {
?>
{
"success": false,
"secret": "Invalid credentials"
}
<?php
}
} else {
?>
{
"success": false,
"message": "Only POST access accepted"
}
<?php
}
?>
{
“成功”:没错,
“秘密”:“这是除了管理员之外没人知道的秘密”
}
{
“成功”:错误,
“机密”:“无效凭据”
}
{
“成功”:错误,
“消息”:“仅接受后期访问”
}
在loginComponent中这样更改代码:
this.Auth.getUserDetails(username, password).subscribe(data => {
if (data && data['success']) {
} else {
window.alert(data['message'])
}
})