PHP服务器端可以';t翻译Angular2 Post请求
我正在开发一个前端使用Angular2的应用程序,访问后端的PHP Restfull API,我不明白为什么通过POST发出的简单登录请求没有被服务器端“翻译” Angular2服务(这里的API路径和一些代码只是为了避免暴露我的真实代码)PHP服务器端可以';t翻译Angular2 Post请求,php,.htaccess,angular,api,post,Php,.htaccess,Angular,Api,Post,我正在开发一个前端使用Angular2的应用程序,访问后端的PHP Restfull API,我不明白为什么通过POST发出的简单登录请求没有被服务器端“翻译” Angular2服务(这里的API路径和一些代码只是为了避免暴露我的真实代码) 从'@angular/core'导入{Injectable}; 从'@angular/Http'导入{Http,Headers,Response}; 从“rxjs”导入{Observable}; 导入'rxjs/add/operator/map'; 从“@a
从'@angular/core'导入{Injectable};
从'@angular/Http'导入{Http,Headers,Response};
从“rxjs”导入{Observable};
导入'rxjs/add/operator/map';
从“@angular/Router”导入{Router}”;
@可注射()
导出类身份验证服务{
构造函数(专用http:http,专用路由器:路由器){
这里有一些代码
}
签名(电子邮件:字符串,密码:字符串):可观察{
还这个
.http
.post('API_PATH/auth',JSON.stringify({email:email,password:password}))
.map(此处有一些代码)
.接住(
(错误:响应)=>{
console.log(错误);
返回可观察抛出(错误);
}
);
}
}
关于.htaccess。我倾向于认为问题就在这里,.htaccess
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase <API_PATH>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ router.php?request=$1 [QSA,L]
</IfModule>
选项-多视图
重新启动发动机
重写基
重写cond%{REQUEST_FILENAME}-D
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-L
重写规则^(+)$router.php?请求=$1[QSA,L]
router.php
<?php
if (!array_key_exists('HTTP_ORIGIN', $_SERVER)) {
$_SERVER['HTTP_ORIGIN'] = $_SERVER['SERVER_NAME'];
}
try {
/*$_REQUEST['request'] brings exactly what I want from .htacces,
that is the endpoint name. But how could access the arguments
(email and password) sent via Angular2 Post?*/
require_once $_REQUEST['request'] . ".php";
$API = new $_REQUEST['request']($_REQUEST, $_SERVER['HTTP_ORIGIN']);
echo $API->processAPI();
} catch (Exception $e) {
echo json_encode(Array('error' => $e->getMessage()));
}
<?php
abstract class API
{
protected $method = '';
protected $endpoint = '';
protected $args = Array();
protected $file = Null;
public function __construct($request) {
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: *");
header("Content-Type: application/json");
$this->args = $request;
$this->endpoint = array_shift($this->args);
$this->method = $_SERVER['REQUEST_METHOD'];
if ($this->method == 'POST' && array_key_exists('HTTP_X_HTTP_METHOD', $_SERVER)) {
if ($_SERVER['HTTP_X_HTTP_METHOD'] == 'DELETE') {
$this->method = 'DELETE';
} else if ($_SERVER['HTTP_X_HTTP_METHOD'] == 'PUT') {
$this->method = 'PUT';
} else {
throw new Exception("Unexpected Header");
}
}
switch($this->method) {
case 'DELETE':
case 'POST':
$this->request = $this->_cleanInputs($_POST);
break;
case 'GET':
$this->request = $this->_cleanInputs($_GET);
break;
case 'PUT':
$this->request = $this->_cleanInputs($_GET);
$this->file = file_get_contents("php://input");
break;
default:
$this->_response('Invalid Method', 405);
break;
}
}
public function processAPI() {
if (method_exists($this, $this->endpoint)) {
return $this->_response($this->{$this->endpoint}($this->args));
}
return $this->_response("No Endpoint: $this->endpoint", 404);
}
private function _response($data, $status = 200) {
header("HTTP/1.1 " . $status . " " . $this->_requestStatus($status));
return json_encode($data);
}
private function _cleanInputs($data) {
$clean_input = Array();
if (is_array($data)) {
foreach ($data as $k => $v) {
$clean_input[$k] = $this->_cleanInputs($v);
}
} else {
$clean_input = trim(strip_tags($data));
}
return $clean_input;
}
private function _requestStatus($code) {
$status = array(
200 => 'OK',
404 => 'Not Found',
405 => 'Method Not Allowed',
500 => 'Internal Server Error',
);
return ($status[$code])?$status[$code]:$status[500];
}
}
它将作为json正文发送,因此它将位于php://input
,如果您像经典帖子一样需要它,那么您需要向ajax调用添加内容类型标题application/x-www-form-urlencoded
。可能重复:可能重复
<?php
abstract class API
{
protected $method = '';
protected $endpoint = '';
protected $args = Array();
protected $file = Null;
public function __construct($request) {
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: *");
header("Content-Type: application/json");
$this->args = $request;
$this->endpoint = array_shift($this->args);
$this->method = $_SERVER['REQUEST_METHOD'];
if ($this->method == 'POST' && array_key_exists('HTTP_X_HTTP_METHOD', $_SERVER)) {
if ($_SERVER['HTTP_X_HTTP_METHOD'] == 'DELETE') {
$this->method = 'DELETE';
} else if ($_SERVER['HTTP_X_HTTP_METHOD'] == 'PUT') {
$this->method = 'PUT';
} else {
throw new Exception("Unexpected Header");
}
}
switch($this->method) {
case 'DELETE':
case 'POST':
$this->request = $this->_cleanInputs($_POST);
break;
case 'GET':
$this->request = $this->_cleanInputs($_GET);
break;
case 'PUT':
$this->request = $this->_cleanInputs($_GET);
$this->file = file_get_contents("php://input");
break;
default:
$this->_response('Invalid Method', 405);
break;
}
}
public function processAPI() {
if (method_exists($this, $this->endpoint)) {
return $this->_response($this->{$this->endpoint}($this->args));
}
return $this->_response("No Endpoint: $this->endpoint", 404);
}
private function _response($data, $status = 200) {
header("HTTP/1.1 " . $status . " " . $this->_requestStatus($status));
return json_encode($data);
}
private function _cleanInputs($data) {
$clean_input = Array();
if (is_array($data)) {
foreach ($data as $k => $v) {
$clean_input[$k] = $this->_cleanInputs($v);
}
} else {
$clean_input = trim(strip_tags($data));
}
return $clean_input;
}
private function _requestStatus($code) {
$status = array(
200 => 'OK',
404 => 'Not Found',
405 => 'Method Not Allowed',
500 => 'Internal Server Error',
);
return ($status[$code])?$status[$code]:$status[500];
}
}
<?php
require_once 'API.php';
class auth extends API
{
public function __construct($request, $origin) {
parent::__construct($request);
}
protected function auth($args) {
//Why I can't find login and password ?
}
}