我有一个 ajax 调用,它正在向我的一个控制器发送一些 ID。
运行此操作的jQuery基本上是搜索具有像notification-id-
这样的ID的元素,然后获取ID并将它们存储在JavaScript变量中。
当我alert()
或console.log()
这个变量时,它会打印出我在页面上的两个通知的1,2
值 notification-id-1
和 notification-id-2
.
对于我的 ajax 调用,我只是在下面做:
$.ajax({
url: "{{ url('/notifications/read') }}",
method: "POST",
data: notifications, // Let jQuery handle packing the data for you
success: function(response) {
// The data was sent successfully and the server has responded (may have failed server side)
alert(notifications);
},
error: function(xhr, textStatus, errorThrown) {
// AJAX (sending data) failed
},
complete: function() {
// Runs at the end (after success or error) and always runs
}
});
我正在尝试通过执行dd($request->all());
来测试我的控制器正在接收的内容,但这只是返回:
array:1 [
"undefined" => ""
]
(ajax 调用成功运行)
如何在控制器中检索在此 ajax 调用中发送的 ID 值?
编辑:完整脚本
<script type="text/javascript">
$(document).ready(function() {
var count = $('#notification-counter');
var new_notifications = $('#notification-new-counter');
$('#notification-drop-down').on('click', function() {
count.empty();
var notifications = $('[id^="notification-id-"]').map(function() {
return this.id.slice(16);
}).get();
$.ajax({
url: "{{ url('/notifications/read') }}",
method: "POST",
data: notifications, // Let jQuery handle packing the data for you
success: function(response) {
// The data was sent successfully and the server has responded (may have failed server side)
alert(notifications);
},
error: function(xhr, textStatus, errorThrown) {
// AJAX (sending data) failed
},
complete: function() {
// Runs at the end (after success or error) and always runs
}
});
});
});
</script>
$.ajax({
url: "{{ url('/notifications/read') }}",
method: "POST",
data: {'notifications':notifications},
你能使用以下行告诉我你在控制器中得到了什么吗?数据:{通知:2}。如果您正确获得此值,则在原始帖子中,您没有正确发送通知,也没有在控制器中得到任何内容。