slim 显示来自其文件的 JSON 响应,调用文件似乎从未得到响应
slim displays JSON response from its file, calling file never seems to get response
正在处理我的第一个 slim 项目,所以我怀疑我缺少一些简单的东西。
这是在 WAMP 开发环境中。
我有一个名为 getValue.php 的文件,它具有简单的形式并将值传递给一个名为 index.php.
的纤细 API 文件
index.php 端的所有处理都在工作,除了 slim 似乎只是在 http://localhost/project/index.php/value 中显示 json 响应,而不是将其传回 getValue.php。我希望 getValue.php 处理响应显示。
这里是getValue.php
<?php
if (!empty($_POST['uid'])) {
$json = 'index.php/value?uid=' . $_POST['uid'];
$arr = file_get_contents($json);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>value</title>
</head>
<body>
<form action="index.php/value" method="post">
<input type="text" name="uid"/>
<button type="submit">Submit</button>
</form>
<br/>
<?php
if(!empty($arr)){
echo json_decode($arr, true);
}
?>
</body>
</html>
这里是index.php
<?php
use \Slim\Slim;
$app = new Slim(array(
'mode' => 'development'
));
$app->post('/value', function () use ($app){
$uid = $app->request->params('uid');
$uid = $uid + 1;
$arr = array("uid" => $uid);
$response = $app->response();
$response['Content-Type'] = 'application/json';
$response->body(json_encode($arr));
});
postman 似乎可以很好地发送、接收和显示响应。
有什么想法吗?
如您所想,事情很简单:您表单中的操作有误。由于您希望 getValue.php
脚本处理 POST,您应该将其用作表单操作。这样:
<form action="getValue.php" method="post">
按照您现在的方式,您 POST 将表单直接转到 Slim 路由,绕过 getValue.php
脚本。
正在处理我的第一个 slim 项目,所以我怀疑我缺少一些简单的东西。
这是在 WAMP 开发环境中。
我有一个名为 getValue.php 的文件,它具有简单的形式并将值传递给一个名为 index.php.
的纤细 API 文件index.php 端的所有处理都在工作,除了 slim 似乎只是在 http://localhost/project/index.php/value 中显示 json 响应,而不是将其传回 getValue.php。我希望 getValue.php 处理响应显示。
这里是getValue.php
<?php
if (!empty($_POST['uid'])) {
$json = 'index.php/value?uid=' . $_POST['uid'];
$arr = file_get_contents($json);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>value</title>
</head>
<body>
<form action="index.php/value" method="post">
<input type="text" name="uid"/>
<button type="submit">Submit</button>
</form>
<br/>
<?php
if(!empty($arr)){
echo json_decode($arr, true);
}
?>
</body>
</html>
这里是index.php
<?php
use \Slim\Slim;
$app = new Slim(array(
'mode' => 'development'
));
$app->post('/value', function () use ($app){
$uid = $app->request->params('uid');
$uid = $uid + 1;
$arr = array("uid" => $uid);
$response = $app->response();
$response['Content-Type'] = 'application/json';
$response->body(json_encode($arr));
});
postman 似乎可以很好地发送、接收和显示响应。
有什么想法吗?
如您所想,事情很简单:您表单中的操作有误。由于您希望 getValue.php
脚本处理 POST,您应该将其用作表单操作。这样:
<form action="getValue.php" method="post">
按照您现在的方式,您 POST 将表单直接转到 Slim 路由,绕过 getValue.php
脚本。