繁体   English   中英

方法不允许。 必须是以下之一:POST - Slim Framework

[英]Method not allowed. Must be one of: POST - Slim Framework

我正在尝试使用php + slim框架编写一个安静的web服务。 它包含一个mongodb小宠物数据库,允许客户搜索有关小宠物的信息。 首先是一个html表单,它收集搜索字段并使用POST方法将其发送到服务器。 在服务器上有以下代码:

$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>

我如何解决它?

问题是您在代码中将POST方法发送到服务器,并且已发送的实际方法是GET。 您是否通过浏览器的URL栏向服务器请求了? 如果是,请使用指定的方法编辑和重新发送数据。 如果没有,请下载适用于开发REST API的Postman for Google Chrome。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM