Jump to content
We've recently updated our Privacy Statement, available here ×
  • Example of using Javascript to schedule a report via REST


    jwhang_1
    • Features: JasperReports Server Version: v7.1.1 Product: JasperReports® Server

    This example demonstrates creating a new scheduled job on JasperServer using Javascript.  For testing the html file was placed within a local JasperServer webapp (../jasperserver-pro) and invoked in a browser using the following URL: http://localhost:8082/jasperserver-pro/ScheduleJobExample.htm.  If the named job definition for the "Employees" sample report does not already exist, a new job will be created within the JasperServer repository and a status code 200 (OK) will be returned to the browser.

    <html>
    <head>
    <script>
        var jobData = '{ \
                "baseOutputFilename": "EmployeeReportSample", \
                "description": "Sample desctiption", \
                "label": "Sample Job Name", \
                "outputFormats": { \
                    "outputFormat": [ \
                        "PDF" \
                    ] \
                }, \
                "outputTimeZone": "America/Los_Angeles", \
                "repositoryDestination": { \
                    "folderURI": "/temp" \
                }, \
                "source": { \
                    "parameters": { \
                        "parameterValues": { \
                        } \
                    }, \
                    "reportUnitURI": "/adhoc/topics/Employees" \
                }, \
                "trigger": { \
                    "simpleTrigger": { \
                        "occurrenceCount": 1, \
                        "startDate": "2050-10-26 10:00", \
                        "startType": 2, \
                        "timezone": "America/Los_Angeles" \
                    } \
                } \
            }';
    
        function scheduleReport(){
            console.log("starting job schedule action.");
            fetch ('http://localhost:8082/jasperserver-pro/rest_v2/jobs', {
                method: 'PUT',
                credentials: 'include',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': 'Basic ' + btoa("jasperadmin|organization_1:jasperadmin"),
                    'X-REMOTE-DOMAIN': '1'
                },
                body: jobData
            })
            .then(response => response.json())
            .catch(error => console.error('Schedule Error:', error))
            .then(response => {
                console.log('Schedule Success:', JSON.stringify(response));
                alert('Job scheduled.');
            });
        }
    
    </script>
    </head>
    
    <body>
    Click button to schedule report<br>
    <button type="submit" onclick="scheduleReport()">Schedule Report</button>
    
    </body>
    </html>

    User Feedback

    Recommended Comments

    There are no comments to display.



    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

×
×
  • Create New...