Ajax,不是作为帖子或获取


Ajax, not as post or get?

我一直在使用 Ajax 主要用作 POST,以照顾思考,但现在只想在 DIV 中打开一个简单的登录页面(并且正在这样做),但现在当我将其添加到 PHP 代码中时出现问题:

<?php
if (isset($_POST))
{
}
else
{
    echo "
    <html>
        <head>
            <meta charset='utf-8'>
            <link rel='stylesheet' type='text/css' href='assets/css/main.css'>
        </head>
        <body>
            <div class='page-header'>
                <h1>Preencha os campos e carregue em enviar</h1>
            </div>
            <form>
                <div class='form-group'>
                    <label for='exampleInputEmail1'>Endereço Email</label>
                    <input type='email' class='form-control' id='exampleInputEmail1' placeholder='Email'>
                </div>
                <div class='form-group'>
                    <label for='exampleInputPassword1'>Palavra-Pass</label>
                    <input type='password' class='form-control' id='exampleInputPassword1' placeholder='Palavra-Pass'>
                </div>
                <button type='submit' class='btn btn-default'>Enviar</button>
            </form>
        </body>
    </html>";
}

这是 Ajax 代码:

$(function() {
    $('.navmenu a').bind('click', function(event) {
        event.preventDefault();
        var urlToGo = $(this).attr('href');
        if ($( window ).width() <= "992" && !$(this).hasClass('dropdown-toggle'))
        {
            $('.navmenu').offcanvas('hide');
        }
        $.ajax({
            url: urlToGo,
            dataType: 'html',
            success: function(result)
            {
                $(".container").html(result);
            }
        });
        $('html,body').animate({
            scrollTop: 0
        }, 700);
        console.log(urlToGo);
    });
});

当我按下用于使用 Ajax 的链接时,它会在div 容器上放置一个空页面,我在这里做错了什么?如果我将 (isset($_POST)) 更改为 (!isset($_POST)),它将正确显示内容,我不希望这是一个帖子。

这里的逻辑有什么问题?!

提前致谢

因为

$_POST是PHP的全局变量,所以它存在于每个php线程中,所以isset($_POST)将返回true,在你的例子中,$_POST的值是一个空数组。你可以尝试一下吹:

if (!empty($_POST)) {
} else {
    echo 'your html'
}

顺便说一下,我认为$_SERVER['REQUEST_METHOD']是判断请求使用哪种http请求方法的更精确的方法。