我正在嘗試使用php + slim框架編寫一個寧靜的webservice。它包含一個小寵物的mongodb數據庫,允許客戶搜索小寵物的相關信息。首先有一個html表單,它收集搜索的字段並用POST方法將它發送到服務器。在服務器有代碼波紋管:方法不允許。必須是其中一個:POST - Slim Framework
$app->post('/', function(Request $req, Response $res){
$n = $req->getParsedBody();
});
,但是當我運行該程序時出現錯誤:
Method not allowed. Must be one of: POST
這裏是服務器上的文件:
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require 'vendor/autoload.php';
$con = new MongoClient("mongodb://localhost:27017");
$db = $con->pokemon;
$colec = $db->pokemon;
$app = new \Slim\App;
// $app->get('/', function($campo) {
// echo $campo."<br>";
// });
$app->post('/', function(Request $req, Response $res){
$n = $req->getParsedBody();
});
$app->run();
?>
下面是HTML文件:
<html>
<head>
<title>Pokedex</title>
</head>
<body>
<link rel="stylesheet" type="text/css" href="estilo.css">
<img id="img" src="../Pokemon/img/pkm.png"/>
<div id="primeiraDiv">
<form id="formulario1" action="servidor.php" method="POST">
<p>Pesquisar Pokemon por nome:</p>
<input type="text" name="nome" id="nome">
<input type="submit" name="botao" id="botao" value="buscar">
</form>
</div>
<div id="segundaDiv">
<form action="servidor.php" method="POST" name="formulario2">
<p>Pesquisar Pokemon por tipo:</p>
<input type="text" name="tipo" id="tipo">
<input type="submit" name="botao" id="botao" value="buscar">
</form>
</div>
<div id="terceiraDiv">
<form action="servidor.php" method="POST" name="formulario3">
<input type="submit" id="listar" nome="listar" value="Listar Todos Pokemons">
</form>
</div>
我該如何解決?
顯示錶單的HTML。 – bcmcfc
退房時,它的上面有 – roooooon
你的表單提交給'servidor.php',但是你的路由是'$ app-> post('/'' – MrCode