【问题标题】:sails.js view does not loadsails.js 视图未加载
【发布时间】:2015-04-02 10:02:11
【问题描述】:

我做了一个控制器,它从一个休息 api 和 将它们序列化为 .xls 文件。

在我的网络视图中,我有一个带有“创建 xls 数据”的按钮,它调用我的控制器。 然后我的控制器序列化并使用 xls 下载链接和“下载数据”按钮重新加载此视图。

我的观点 import_export_data_page.ejs:
在开始按钮显示“创建 xls 数据”。

<%

if(typeof link == 'undefined'){
%>
<button type="button" class="btn btn-primary" id="button_get_xls_file">Create xls data</button>
<%
}else {
%>
<a href="<%= link %>"><button type="button" class="btn btn-primary"></button></a>
<%
}
%>

<script type="text/javascript">
$('#button_get_xls_file').click(function(){
var projectName = getCurrentServiceName(currentService);
$.get( "/api/export_xls_data", { url : currentService, projectname :     projectName} ).done(function( data ) {
  });
});

</script>

在我的控制器中:

exportXLS : function(req,res)
{
    var request = require('request');
    var categories;
    //get informations
     request(url + "/places", function (error, response, body) {
          categories = JSON.parse(body);
         /*
         * Parsing tacks 3 seconds max
         */
        //if file exists delete and create file
        fs.exists(fileName, function (exists) {
                if(exists){
                    fs.unlinkSync(fileName);
                }
                fs.writeFile(fileName, xls, 'binary',function(err){
                if(err){
                      console.log(err);
                      res.status(500);
                     return res.send("");
                }else{
                    var downloadLink = req.headers.host+'/xls/'+currentProject.split(' ').join('')+'_data.xls';
                     console.log(downloadLink);
                     return res.view('import_export_data_page',{link : downloadLink});
                    });
                }); 

    }

}

我的文件已创建。我的链接有效,但我的视图没有重新加载。

【问题讨论】:

    标签: node.js view sails.js reload ejs


    【解决方案1】:

    您的视图未重新加载,因为您进行了 AJAX 调用。您的代码中没有任何内容可以告诉页面重新加载视图。

    我想你想改变这个:

    $.get( "/api/export_xls_data", { url : currentService, projectname :     projectName} ).done(function( data ) {});
    

    到:

    window.location.href = '/api/export_xls_data?url=' + currentService + '&projectname=' + projectName
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-25
      • 2012-08-23
      • 2020-12-06
      • 2019-02-19
      • 2017-01-14
      • 2015-12-25
      • 2013-01-10
      • 1970-01-01
      相关资源
      最近更新 更多