流明阿贾克斯后阿贾克斯后

2023-09-11 01:11:48 作者:眼泪后的坚强

我开发使用框架的Web应用程序流明。我看着无处不在,我什么都试过,但我无法找到一个解决我的问题。

I am developing a web application using the Framework Lumen. I looked everywhere, I tried everything but I couldn't find a solution to my problem..

我有,我想用ajax()与 POST 来验证与jQuery的形式。我试图用 csrf_token ,但始终没有成功。

I have a form that I want to validate with jQuery using ajax() with POST. I tried to use csrf_token, but always without success.

查看

<form id="form">
    <div class="form-group">
        <input type="text"  class="form-control" name="test" placeholder="Test">
    </div>
    <button type="button" class="btn btn-primary btn-block valide">Submit</button>
</form>

JS

$('.valide').click(function () {
    var form = $(this).parents('form');
    $.ajax({
        type: 'POST',
        url: '/testAjax',
        dataType: 'JSON',
        data : form.serialize()
    }).done(function (data) {
        // done
    }).fail(function () {
        // fail
    });
});

routes.php文件

$app->post('/testAjax', function () {
    return 'I am here';
});

一般情况下我得到的消息我在这里,而是,我得到以下错误(请注意,如果我使用提出同样的要求 GET ,它效果非常好):

Normally I would get the message "I am here", but instead, I get the following error (Please note that if I make the same request using GET, it works very well):

POST的http://本地主机/ testAjax 500(内部服务器错误)

如何使一个ajax POST 使用流明的要求吗?

How to make an ajax POST request using Lumen?

推荐答案

我找到了解决办法。简单地添加以下code键HTML格式,允许用户关联到当前形式。这是在文档描述。

I found the solution. Simply add the following code to the HTML form, allowing the user to be associated to the current form. This is described in the documentation.

查看

<form id="form">
    <!-- Added the following line -->
    <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
    <div class="form-group">
        <input type="text"  class="form-control" name="test" placeholder="Test">
    </div>
    <button type="button" class="btn btn-primary btn-block valide">Submit</button>
</form>