admin管理员组文章数量:1122832
web.php:
Route::delete('/roles/{id}/delete', [RoleController::class, 'delete'])->name('roles.delete');
RoleController.php:
public function delete($id): string
{
$data = [
'id' => $id
];
return response()->json($data, 200);
}
index.blade.php:
<a href="{{ route('roles.delete', $role->id) }}" class="btn btn-danger btn-xs btn-action" data-bs-toggle="tooltip" data-bs-custom-class="ls-tooltip" title="{{ __('Delete') }}" data-action-message="{{ __('Delete this role?') }}"><i class="fas fa-trash"></i></a>
<div class="modal fade" id="confirm_action">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header bg-danger">
<h1 class="modal-title fs-5 text-white">{{ __('Please confirm') }}</h1>
</div>
<div class="modal-body">
<div class="modal-message"></div>
<div class="mt-1 d-flex justify-content-end">
<form id="form_delete" method="post">
@csrf
@method('DELETE')
<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">{{ __('Cancel') }}</button>
<button type="submit" class="btn btn-danger ms-2">{{ __('Delete') }}</button>
</form>
</div>
</div>
</div>
</div>
</div>
app.js:
const confirm_action = document.getElementById('confirm_action');
const btn_action = document.querySelectorAll('.btn-action');
if (typeof(confirm_action) != 'undefined' && confirm_action != null) {
const modal = new bootstrap.Modal(confirm_action);
const form = document.getElementById('form_delete');
let url, id, message;
confirm_action.addEventListener('show.bs.modal', event => {
const confirm_text = confirm_action.querySelector('.modal-message');
confirm_text.textContent = message;
});
form.addEventListener('submit', event => {
event.preventDefault();console.log(url);
axios.delete(url, {
data: {
id: id,
},
}).then(response => {
if (response.status === 200) {
let data = response.data;
console.log(data);
}
}).catch(error => {
console.log(error);
});
});
btn_action.forEach(btn => {
btn.addEventListener('click', event => {
event.preventDefault();
url = btn.getAttribute('href');
id = btn.getAttribute('data-role-id');
message = btn.getAttribute('data-action-message');
modal.show();
});
});
}
I get the following response as a string:
HTTP/1.0 200 OK
Cache-Control: no-cache, private
Content-Type: application/json
Date: Sat, 23 Nov 2024 07:07:09 GMT
{"id":"7"}
But how do I get the data 'id'?
web.php:
Route::delete('/roles/{id}/delete', [RoleController::class, 'delete'])->name('roles.delete');
RoleController.php:
public function delete($id): string
{
$data = [
'id' => $id
];
return response()->json($data, 200);
}
index.blade.php:
<a href="{{ route('roles.delete', $role->id) }}" class="btn btn-danger btn-xs btn-action" data-bs-toggle="tooltip" data-bs-custom-class="ls-tooltip" title="{{ __('Delete') }}" data-action-message="{{ __('Delete this role?') }}"><i class="fas fa-trash"></i></a>
<div class="modal fade" id="confirm_action">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header bg-danger">
<h1 class="modal-title fs-5 text-white">{{ __('Please confirm') }}</h1>
</div>
<div class="modal-body">
<div class="modal-message"></div>
<div class="mt-1 d-flex justify-content-end">
<form id="form_delete" method="post">
@csrf
@method('DELETE')
<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">{{ __('Cancel') }}</button>
<button type="submit" class="btn btn-danger ms-2">{{ __('Delete') }}</button>
</form>
</div>
</div>
</div>
</div>
</div>
app.js:
const confirm_action = document.getElementById('confirm_action');
const btn_action = document.querySelectorAll('.btn-action');
if (typeof(confirm_action) != 'undefined' && confirm_action != null) {
const modal = new bootstrap.Modal(confirm_action);
const form = document.getElementById('form_delete');
let url, id, message;
confirm_action.addEventListener('show.bs.modal', event => {
const confirm_text = confirm_action.querySelector('.modal-message');
confirm_text.textContent = message;
});
form.addEventListener('submit', event => {
event.preventDefault();console.log(url);
axios.delete(url, {
data: {
id: id,
},
}).then(response => {
if (response.status === 200) {
let data = response.data;
console.log(data);
}
}).catch(error => {
console.log(error);
});
});
btn_action.forEach(btn => {
btn.addEventListener('click', event => {
event.preventDefault();
url = btn.getAttribute('href');
id = btn.getAttribute('data-role-id');
message = btn.getAttribute('data-action-message');
modal.show();
});
});
}
I get the following response as a string:
HTTP/1.0 200 OK
Cache-Control: no-cache, private
Content-Type: application/json
Date: Sat, 23 Nov 2024 07:07:09 GMT
{"id":"7"}
But how do I get the data 'id'?
Share Improve this question edited Nov 23, 2024 at 7:11 linuxoid asked Nov 23, 2024 at 1:12 linuxoidlinuxoid 1,4473 gold badges16 silver badges35 bronze badges 4 |2 Answers
Reset to default 0Your problem is your method:
public function delete($id): string
// ^ this
{
$data = [
'id' => $id
];
return response()->json($data, 200);
}
The return type of the method is string
, and according to the php documentation, php will coerce types to the type expected; in this case, string
.
Normally Laravel uses the Illuminate\Http\Response
type. I don't know enough about Laravel internals to describe why it, or Symfony, needs that specific type of object to correctly output the response as you want it.
The fix in this case is to of course remove the type coercion from the method, or set it to the correct value.
I'm sure that your url = undefined
at axios.delete(url, {
-> axios will get url="your web url"
-> and "your web url"
just Supported methods: GET, HEAD, POST."
Solution:
Add hidden url
field in form
<form id="form_delete" method="post">
@csrf
@method('DELETE')
<input type="hidden" id="form_url" />
<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">{{ __('Cancel') }}</button>
<button type="submit" class="btn btn-danger ms-2">{{ __('Delete') }}</button>
</form>
Button clicked => set url
for hidden field
Change form from addEventListener
to submit
event
<form id="form_delete" onsubmit="yourSubmitFn()" method="post">
Because when you add event listener => js will store old url
and after that if you change url => js still keep old url
value
本文标签: axiosLaravel 11 delete item with Bootstrap 5 modalresponse is stringnot objectStack Overflow
版权声明:本文标题:axios - Laravel 11 delete item with Bootstrap 5 modal - response is string, not object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736300150a1930714.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
string
, thus coercing the response to a string. – Daedalus Commented Nov 23, 2024 at 7:17