javascript - 如何将控制台日志结果传递给我的 ajax,以便我可以将其插入 DB?
问题描述
我正在做一个配对系统。我的问题是,如何将我的 console.log 结果插入我的数据库?当我的数据库中有相同级别时,console.log 的结果就会发生。
, 例如,测试 4的级别为 1,测试 1 的级别为 1。它们将加入 1 个数组,因为它们具有相同的级别
其他人也一样。我在下面提供了我的代码和目标的屏幕截图。任何帮助将不胜感激。非常感谢。
ajax 获取:
<script>
let ajaxResult = []; // the pushed data will be saved here
let table;
let base_url = "<?php echo base_url();?>";
let result = [];
const combine = (source) => {
return source[0].data.reduce((acc, curr) => {
const [entryID, entryName, level] = curr;
if (acc[level])
acc[level].push({
entryID,
entryName,
level
});
else
acc[level] = [{
entryID,
entryName,
level
}];
return acc;
}, {})
}
$(document).ready(function() {
//datatables
table = $("#entry_list1").DataTable({
processing: false,
serverSide: true,
order: [],
searching: false,
paging: false,
info: false,
ajax: {
url: "<?php echo site_url('controller/fetch_players')?>",
type: "POST",
async: true,
dataType: "json",
success: function(data) {
ajaxResult.push(data); // I'm pushing my data to my ajaxResult variable
result = combine(ajaxResult); // Cleanup your data here.
console.log(combine(ajaxResult)); // The result in image i provided above is from here.
},
},
"columnDefs": [{
"targets": [0], //first column
"orderable": false, //set not orderable
},
{
"targets": [-1], //last column
"orderable": false, //set not orderable
},
],
});
});
</script>
阿贾克斯插入:
// i want to pass my ajaxResult = []; here so that i can insert it into my database
<script type="text/javascript">
$("#insertMe").submit(function(event) {
var form = $('#insertMe')[0];
var formData = new FormData(form);
event.preventDefault();
$.ajax({
url: "<?php echo base_url('controller/insertFunction'); ?>",
cache: false,
contentType: false,
processData: false,
data: formData,
type: "post",
async: false,
dataType: 'json',
success: function(data){
},
});
});
</script>
控制器:
function inserFunction() {
$this->db->insert('matching', $data);
}
解决方案
推荐阅读
- ios - 不推荐使用 UnsafeBytes
- php - 如何在没有注册和对象管理器的情况下在 Magento 2 中获取当前产品?
- javascript - 将相同的文件上传到 Azure Blob 存储而不覆盖?
- php - 使用 codeigniter 取消链接和删除数据库行的问题
- dialogflow-es - 对话流中的 Webhook 安全性
- jquery - Owl Carousel - 使用填充比率技巧 - 将图像居中对齐
- python - Python Re apply/search TypeError: 'NoneType' object is not subscriptable
- html - HTML 5 视频 CSS:在视频海报图像上强制响应比率 16:9(对于 preload="none" 视频)
- flutter - Flutter :- 如何在屏幕上显示动态小部件?
- sql - 系统视图优化