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
}
}
发布于 2018-07-02 01:02:52
我遵循了本教程,遇到了同样的问题,我不得不将此代码添加到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);
https://stackoverflow.com/questions/51112592
复制相似问题