首页 > 解决方案 > 如何导出响应 AJAX、Jquery

问题描述

我发现了一个问题,但我不知道如何解决它。

这是关于 AJAX(jquery) 请求,在用户控制器内部,应该将结果“导出”到主控制器。

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>

标签: javascriptjqueryajaxnpmexport

解决方案


推荐阅读