流明 ajax post
Lumen ajax post
我正在使用框架 Lumen 开发 Web 应用程序。我到处寻找,尝试了所有方法,但找不到解决问题的方法..
我有一个表单,我想使用 ajax() 和 POST
通过 jQuery 验证。我尝试使用 csrf_token
,但始终没有成功。
VIEW :
<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';
});
通常我会收到消息 "I am here",但我收到以下错误(请注意,如果我使用 GET
发出相同的请求,它工作得很好):
POST http://localhost/testAjax 500 (Internal Server Error)
如何使用 Lumen 发出 ajax POST
请求?
改变路线
$app->post('/testAjax', function () {
return 'I am here';
});
到
$app->post('testAjax', function () {
return 'I am here';
});
我找到了解决方案。只需将以下代码添加到 HTML 表单,即可将用户关联到当前表单。 documentation.
中对此进行了描述
VIEW :
<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>
此外,如果有人不愿通过表单发送 jQuery POST,您只需在下面的脚本代码中添加:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
记得在您的视图中包含此 html:
<meta name="csrf-token" content="{{ csrf_token() }}">
我正在使用框架 Lumen 开发 Web 应用程序。我到处寻找,尝试了所有方法,但找不到解决问题的方法..
我有一个表单,我想使用 ajax() 和 POST
通过 jQuery 验证。我尝试使用 csrf_token
,但始终没有成功。
VIEW :
<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';
});
通常我会收到消息 "I am here",但我收到以下错误(请注意,如果我使用 GET
发出相同的请求,它工作得很好):
POST http://localhost/testAjax 500 (Internal Server Error)
如何使用 Lumen 发出 ajax POST
请求?
改变路线
$app->post('/testAjax', function () {
return 'I am here';
});
到
$app->post('testAjax', function () {
return 'I am here';
});
我找到了解决方案。只需将以下代码添加到 HTML 表单,即可将用户关联到当前表单。 documentation.
中对此进行了描述VIEW :
<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>
此外,如果有人不愿通过表单发送 jQuery POST,您只需在下面的脚本代码中添加:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
记得在您的视图中包含此 html:
<meta name="csrf-token" content="{{ csrf_token() }}">