首页 > 解决方案 > http://localhost:4200/api/auth.php 404(未找到)

问题描述

app.module.ts

import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Headers, Http } from '@angular/http';

@Injectable({ providedIn: 'root' }) export class AuthService {

constructor(private http: HttpClient) { }

getUserDetails(username, password){ return this.http.post('/api/auth.php',{ username, password }).subscribe(data => { console.log(data, "Is what we got from the server"); }) } }

proxyconfig.json

{ "/api": { "target": "http://localhost/test/api/auth.php", "secure": false, "changeOrigin": true

}
}

标签: javascriptangular

解决方案


我正在关注本教程并且遇到了同样的问题,我必须将此代码添加到 auth.php 文件的开头;

header('Access-Control-Allow-Origin: *'); 
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, 
Accept");

所以前五行现在看起来像;

<?php
header('Access-Control-Allow-Origin: *'); 
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, 
Accept");
session_start();
$_POST = json_decode(file_get_contents('php://input'), true);

推荐阅读