【发布时间】:2018-04-03 07:43:12
【问题描述】:
我正在尝试通过 ajax 请求发布一些数据。
这是我 view.blade.php 中的 javascript 代码,它引用了http://mysite/element/edit/{id}
<script>
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$(function() {
$( "#append-content" ).sortable({ handle: '.composer-row-header-handle' }).bind('sortupdate', function(e, ui) {
var rowID = $('.composer-row').map(function(){
return $(this).attr("id");
}).get();
$.ajax({
type: "POST",
url: "sort/store",
dataType: "json",
data: {
rowID: rowID
},
success: function(order){
console.log(rowID)
},
error: function(){
console.log(rowID)
}
});
});
});
</script>
在同一个文件中,我插入了相对的 _token 元数据
<meta name="csrf-token" content="{{ csrf_token() }}">
然后我在路由文件中设置了一个 POST 路由
Route::post('element/edit/sort/store', 'ElementsController@sort');
而我的 ElementsController 排序功能是
public function sort(Request $request)
{
$rowID = $request->input('rowID');
$i = 1;
foreach($rowID as $val) {
$val = str_replace("row-", "", $val);
DB::table('element')
->where('refID', 1)
->where('rowID', $val)
->update(
[
'rowORDER' => $i,
]
);
$i++;
}
}
但是当我尝试重新排序日志时,响应是
jquery-1.12.0.min.js:4 POST http://mysite/element/edit/sort/store 500 (Internal Server Error)
如果我尝试在新页面中打开链接,这就是结果
提前感谢您的每一个回答
【问题讨论】:
-
当您尝试在新页面中打开链接时,您使用的是
GET方法而不是POST,所以您得到了MethodNotAllowedHttpException。至于 ajax 调用,只需检查您的 laravel 日志文件即可。 -
但是我指定 ajax 使用类型
POST -
您是否检查过您的 ajax 在哪条路线上命中?是撞到了你在评论中提到的路线,还是撞到了其他东西?
标签: javascript jquery ajax laravel post