簡體   English   中英

如何通過URL傳遞帶有對象值的參數

[英]How to pass parameters with Object Values through URL

我創建了一個項目,該項目允許將HTML轉換為PDF。 我正在使用phantom-render-stream將HTML轉換為PDF。 現在的問題是,我無法將對象值傳遞給HTML。 下面是我的代碼。

app.js

var username = "AAA";
var pets = [
   {id: 1, name: "cat"},
   {id: 2, name: "dog"},
   {id: 3, name: "sugar glider"}
];
var address = {
    line1: "Street A123",
    town: "Edinburgh",
    country: "Scotland"
};

render('http://localhost:8888/createForm.html?username='+username+'&pets='+pets+'&address='+address
.pipe(fs.createWriteStream('customer.pdf'));

createForm.html ,我使用了AngularJS來獲取和查看PDF的值。

createForm.html

<script>
        var app = angular.module('myApp', []);

        app.config(function($locationProvider){
            $locationProvider.html5Mode(true);
        });

        app.controller('getDataCtrl', function($scope, $location) {
                         var url = decodeURIComponent( $location.url() );
             var value = $location.search();

            $scope.username = value.username;
            $scope.pets = value.pets;
                       $scope.address = value.address;
            })
</script>

<body>
      <div ng-app="onePayForm" ng-controller="getDataCtrl">
           <p>{{username}}</p>
           <p>{{pets[0].name}}</p>
           <p>{{address.line1}}</p>
      </div>
</body>

將HTML成功轉換為PDF后,我打開了pdf文件以查看結果。 pdf僅顯示用戶名,其余顯示為{{pets[0].name}} {{address.line1}}

render('http://localhost:8888/createForm.htmlusername='+username+'&pets='+pets+'&address='+address)
    .pipe(fs.createWriteStream('customer.pdf'));

您需要在將查詢參數附加到url之前進行准備。 你應該怎么做

var readyPets = encodeURIComponent(JSON.stringify(pets));
var readyAddress = encodeURIComponent(JSON.stringify(address));

然后將上面的代碼更改為:

render('http://localhost:8888/createForm.htmlusername='+username+'&pets='+readyPets+'&address='+readyAddress)
        .pipe(fs.createWriteStream('customer.pdf'));

在createForm.html中解析以下參數查詢:

$scope.pets = JSON.parse(decodeURIComponent(value.pets));
$scope.address = JSON.parse(decodeURIComponent(value.address));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM