javascript - 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
}
}
解决方案
我正在关注本教程并且遇到了同样的问题,我必须将此代码添加到 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);
推荐阅读
- java - TreeContentProvider 只是获取我路径的每个目录。(文件过滤器)
- python - Python:使用正则表达式定义边界
- javascript - 试图添加。8 到以前的总和
- python - 如何在 Python 中移动 DataFrame 列的数字或字符串中的位置点?
- javascript - 如何以角度从另一个组件销毁活动路由组件
- laravel-7 - 为 foreach() 提供的参数无效(查看:C:\xampp\htdocs\E-Commerce\resources\views\orders\order.blade.php)
- angular - 全局变量值不变
- java - 使用 java 从 Azure devops 将 git 存储库克隆到本地
- typescript - 如何避免“未定义”添加以下打字稿字符串?
- javascript - 为什么异步功能不起作用是 chrome.storage 回调?