javascript - 如何导出响应 AJAX、Jquery
问题描述
我发现了一个问题,但我不知道如何解决它。
这是关于 AJAX(jquery) 请求,在用户控制器内部,应该将结果“导出”到主控制器。
- main.controller.js = 接收包含所有数据的表
- users.controller.js = import view + (async ajax: addRows to table) + return
- user.view.html = 只有模式(table->tbody)
CODIGO DEL 用户控制器:
import view from '../view/users.html';
export default () =>{
var remoteData = $.ajax({
url: '/mobi',
type: 'post',
cache: false
}).done( function (data){
response(data);
}).fail(function (data, status, exception){
alert(exception);
}).always(function (){
console.log('Fin');
});
function response(data){
var $viewtable = $(view);
data.data.forEach(function (datauser){
var $row_new =
'<tr>' +
'<td>' + datauser.name + '</td>' +
'<td>' + datauser.user + '</td>' +
'<td>' + datauser.email + '</td>' +
'<td>' + datauser.active + '</td>' +
'<td>' + datauser.ts_create + '</td>' +
'</tr>'
;
$viewtable.find('tbody').append($row_new);
});
return $viewtable.html;
}
return ???; // here is where i don't know how to raise the response to main.controller.
}
CODIGO DEL 主控制器:
switch (route) {
case "#users": {
$.when(pages.users()).then(function (data){ //Intento usar promesa para asegura la respuesta??
$(content).html(data); //Obviamente 'undefined'
});
}
default: {
return $(content).append(pages.notFound());
}
}
CODIGO 视图:
<table id="usersTable" class="table table-sm table-hover">
<caption>List of users</caption>
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">username</th>
<th scope="col">mail</th>
<th scope="col">active</th>
<th scope="col">created</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
解决方案
推荐阅读
- node.js - Node Js pm2 现在重启
- apollo-server - 如何在 Apollo GraphQL Server 上同步使用 Knexjs
- python - argparse:具有全局参数的子解析器,位置无关紧要
- c++ - 使用 .txt 文件将数据集输入到 C++ 中的向量(可能不止一个数据集)中
- angular - Angular Route 部分通配符
- javascript - 模拟真实用户点击绕过网络蜜罐
- c# - 你如何正确地发送一个 json 对象到 php 后端
- c++ - 检查(全局)函数是否存在,但不允许隐式转换
- typescript - Typescript 将 JS 数组视为对象
- python - Python:只允许设置具有@property 装饰器的属性