尝试使用 axios 上的 put 提交数据但不允许使用 405 方法
Trying to submit data using put on axios but got 405 method not allowed
我正在尝试在 jquery 代码中使用 PUT 方法提交
$('#mainTable td').on('change', function(evt, newValue) {
var $put_method = $('#user-put-method input').val();
var $name = $(this).attr('id');
var $url = "<?php echo route('Profile.update', Auth::id()) ?>";
var data = {
'__method': $put_method,
'name': $name
};
axios.defaults.headers.common['X-CSRF-TOKEN'] = document.querySelector('meta[name="csrf-token"]').getAttribute('content')
axios.post($url, data)
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
});
此var $put_method = $('#user-put-method input').val();
的数据来源来自
<div class="" id="user-put-method">
{{ method_field('PUT') }}
</div>
我有这个方法来捕获请求
public function update(Request $request, $id)
{
d($request, $id);
}
并通过这条路线
Route::resource('Profile', 'UserProfileController');
但是当我查看 chrome 开发工具时
我收到这条消息
http://project.dev/Profile/1 405 method not allowed
有没有人遇到或做过类似我的问题?
在你的HTML头文件中添加这个脚本:
<script>
window.Laravel = <?php echo json_encode([
'csrfToken' => csrf_token(),
'base_url' => \URL::to('/'),
]); ?>
</script>
还有你的 axios 数据:
var data = {
'__method': $put_method,
'name': $name,
'_token': window.Laravel.csrfToken
};
// axios.defaults.headers.common['X-CSRF-TOKEN'] = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
axios.put($url, data)
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
我正在尝试在 jquery 代码中使用 PUT 方法提交
$('#mainTable td').on('change', function(evt, newValue) {
var $put_method = $('#user-put-method input').val();
var $name = $(this).attr('id');
var $url = "<?php echo route('Profile.update', Auth::id()) ?>";
var data = {
'__method': $put_method,
'name': $name
};
axios.defaults.headers.common['X-CSRF-TOKEN'] = document.querySelector('meta[name="csrf-token"]').getAttribute('content')
axios.post($url, data)
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
});
此var $put_method = $('#user-put-method input').val();
的数据来源来自
<div class="" id="user-put-method">
{{ method_field('PUT') }}
</div>
我有这个方法来捕获请求
public function update(Request $request, $id)
{
d($request, $id);
}
并通过这条路线
Route::resource('Profile', 'UserProfileController');
但是当我查看 chrome 开发工具时 我收到这条消息
http://project.dev/Profile/1 405 method not allowed
有没有人遇到或做过类似我的问题?
在你的HTML头文件中添加这个脚本:
<script>
window.Laravel = <?php echo json_encode([
'csrfToken' => csrf_token(),
'base_url' => \URL::to('/'),
]); ?>
</script>
还有你的 axios 数据:
var data = {
'__method': $put_method,
'name': $name,
'_token': window.Laravel.csrfToken
};
// axios.defaults.headers.common['X-CSRF-TOKEN'] = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
axios.put($url, data)
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});