我想在我的页面中发送一个ajax请求,但我不知道如何。 这是我的代码:
<script type="text/javascript">
$(document).ready(function(){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': {!! json_encode(csrf_token()) !!},
}
});
$('.del_btn').on('click', function(){
var id = $(this).data('id');
$.ajax({
url: "posts/something",
type: 'post',
dateType: 'json',
data: {name: 'rochelle'},
beforeSend: function() { console.log('loading'); },
success: function(d) {
console.log(d.test);
},
error: function() { alert('error'); }
});
});
});
</script>
然后我注册了路线:
Route::get('posts/something', 'PostsController@something');
在我的 Controller 中我有这个:
public function something() {
$test = $request->input('name');
return Response::json($test);
}
我在开发控制台选项卡中收到此错误:
405 Method Not Allowed
你能帮我解决这个问题吗?我是 Laravel 的新手。
最佳答案
您正在发帖,但 laravel 路由设置为期望获取。
更改:
Route::get('posts/something', 'PostsController@something');
至:
Route::post('posts/something', 'PostsController@something');
关于php - 如何在 Laravel 5 中使用 AJAX 发送数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32222414/