http:// localhost:4200 / api / auth.php 404(未找到)

时间:2018-06-30 07:06:09

标签: javascript angular

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

}
}

1 个答案:

答案 0 :(得分:0)

我一直在学习本教程,并且遇到相同的问题,我必须将此代码添加到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);