【发布时间】:2017-10-10 12:45:36
【问题描述】:
我正在尝试从 JS 文件到 laravel 路由发布 jquery 帖子,但似乎它不起作用,我不知道为什么。
我的主要目标是:从选中了复选框的表中获取所有 ID,并在 SQL 上更改它们的列值。
所以,这是我的 JS 函数:
function concludeAll() {
var arrayIds = [];
$('.checkbox1').each(function() {
var $this = $(this);
var $id = $this.attr('id');
if ($this.is(":checked")) {
arrayIds.push($id);
}
});
var json = {
"ids": arrayIds
};
$.post('http://localhost:8000/controle/pending/concludeAll',
{
'_token': $('meta[name=csrf-token]').attr('content'),
ids: arrayIds
})
.error(
)
.success(
);
}
这是我的路线:
Route::group(['prefix' => '/controle'], function() {
Route::post('/pending/concludeAll/', function() {
$input = Input::only('ids');
$input = $input['ids'];
foreach($input as $id) {
$student = new App\Aluno();
$student = $student->where('id', '=', $id)->first();
$student->pending = '0';
$student->save();
}
}); };
因此,如果我检查表格上的几行并点击调用该函数的按钮,我的控制台上不会发生任何事情。在网络 > 标头 > 表单数据上,我看到了令牌和 ID,如下所示:
_token:fNWunwF8yDLSycrkBE684wgQcyK9dP8wbR7VgLjC 身份证[]:23 ids[]:20
在预览时,我看到的页面完全相同。 在响应时,我看到了页面的 HTML。
我也试过 dd($input);在路线上,但没有什么不同..
尝试了 php artisan route:clear 并没有发生任何不同。
如果我更改http://localhost:8000/controle/pending/concludeAll2 的url 名称,则不会返回任何错误,这让我抓狂...
任何想法如何使这个帖子调用路线?谢谢!
【问题讨论】:
-
把你的
arrayIds转换成JSONids: JSON.stringify(arrayIds)然后看看... -
如果你的控制台什么也没发生,你怎么能看到那些标题?
-
@HimanshuUpadhyay 它的唯一区别是:在网络 > 标题 > 表单数据我看到 ids:["23","20","29"] 而不是 ids[]:23 ids[ ]:20。我认为这是必要的,但还没有解决问题。不过还是谢谢。
-
@Devon 在控制台上什么都没有,但在 Chrome 上我去网络 > 我看到一个标题选项卡。
-
我认为你应该从 url 中删除
controle:像这样$.post('http://localhost:8000/pending/concludeAll'
标签: javascript php jquery laravel post