【问题标题】:Typescript: How to write the response in browserTypescript:如何在浏览器中编写响应
【发布时间】:2016-10-28 06:11:56
【问题描述】:

与 Response.Write() 类似的是它们在类型脚本中的任何等效语法

我正在从 typescript 调用 web api,它返回带有内容作为文件附件的 httpResponse 消息,我只是想通过 typescript 在浏览器中打开该附件文件

Public DownloadFile()
{
      var content = //Web API result        
      //Neeed to render this above content in browser      
}

【问题讨论】:

    标签: asp.net-mvc typescript steam-web-api


    【解决方案1】:

    here的简单方法:

    document.write("<h1>result 1</h1>");
    document.write("<h1>result 2</h1>");
    

    如果您想在页面上的 DOM 中获取结果,那么如果您使用的是 jquery,请执行以下操作:

    $(document.body).text(‘Result 1’);
    

    如果你有多行结果:

    $('<p>').text('result 1').appendTo($(document.body));
    $('<p>').text('result 2').appendTo($(document.body));
    

    这可能是这样的方法:

    write(element: JQuery, text: string[]) {
                text.forEach(v => {
                    $('<p>').text(v).appendTo(element);
                });
            }
    

    你可以像这样使用它:this.write($('body'), ['result 1', 'result 2']);

    如果不使用jquery,则编写如下方法:

    write(element: HTMLElement, text: string[]) {
                text.forEach(v => {
                  document.body.innerHTML += v + '<br>';
                });
            }
    

    然后这样称呼它:this.write(document.body, ['result 1', 'result 2']);

    如果您只想在控制台中查看结果,可以写console.log(’result 1’, ’result 2’)。您可以通过按 F12 在类似 Google Chrome Developer 的控制台中查看结果。

    【讨论】:

      猜你喜欢
      • 2020-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多