【问题标题】:How to receive the data that is being passed from client app to the jsreport server如何接收从客户端应用程序传递到 jsreport 服务器的数据
【发布时间】:2017-03-02 16:15:53
【问题描述】:

我正在尝试从连接到 jsreport 的 Angular 应用程序生成 pdf 报告。我的客户端应用程序通过以这种方式将示例数据传递到报表服务器来执行 POST 调用。

 $http.post('http://localhost:5488/api/report', {
      'template': {
        'shortid': 'SypJSv75e',
        "data": {"name": "John Doe"}
      }
    })
    .success(function (response) {
     console.log(response)
    });

正如您在上面的代码中看到的,我将 {"name": "John Doe"} 传递给报表服务器。

在报表服务器上,这是我在自定义脚本部分中的代码。

function beforeRender(req, res, done) {
req.data.generatedOn = new Date();
done();
}

如何接收从客户端应用程序传递的 jsreport 中的数据?

【问题讨论】:

  • 这个问题你解决了吗?

标签: javascript jquery angularjs post jsreport


【解决方案1】:

data 属性不应位于 template 内,您的请求应如下所示

$http.post('http://localhost:5488/api/report', {
      template: { shortid: 'SypJSv75e' },
      data: { name: "John Doe"}
    })
    .success(function (response) {
     console.log(response)
    });

然后您可以使用{{name}} 或在自定义脚本中访问模板内容中的“John Doe”

function beforeRender(req, res, done) {
  //prints into debug John Doe
  console.log(req.data.name)
  done();
}

您可以稍后考虑使用jsreport browser javascript client 来调用渲染调用。

【讨论】:

  • 假设传入的数据是一个对象数组的JSON.stringify,例如:0: 'title', 1: [ { content: 'content', content2: 'content2'} ] 可以调用吗并以同样的方式通过它?比如:"data": { "data": data }, 我会在beforeRender 函数中通过var datapassed = req.data.data 传递它,然后我可以访问datapassed[0] 和datapassed[1] 以获得'string' 和json 内容,对吗?据我在使用调试时可以尝试,它已成功通过。届时如何替换 xlsx 模板中使用的现有示例数据?我尝试使用无法调用的 {{{title}}}。
  • 我也尝试过使用Object.assign(req.data, datapassed);,但它没有传递给模板样本数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-25
  • 2014-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多