Toggle navigation
Run LIVE DEMO
Show CODE
LIVE DEMO PAGE
Updated: 19.06.18 11:10
Adding headers to API calls
<!DOCTYPE html> <html> <head> <title>A very simple smartapp2.js example</title> <!-- jQuery --> <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> <!-- Bootstrap --> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <!-- Smartapp --> <script src="https://smartapp.ikx.no/downloads/jquery.smartapp2.js"></script> </head> <body> <div id="AppContainer"></div> <!-- View1 added through markup --> <div id="View1" class="text-center"> <h1>This demo adds a header to API calls</h1> <p>And the API call doesn't actually work.</p> <button class="btn btn-default" id="CallApi">Send an API Get message</button> </div> <script> $(function() { // Initialize app on a DOM container element var $app = $("#AppContainer").smartapp2("https://serviceurl/"); // Register a view on URL #View1 (will be default view, as it is registered first) $app.createView("View1", $("#View1"), function($me) { $me.on("viewinit", function() { $me.CallApi.on("click", function() { $app.APIGet("Controller", "Function", { testdata: "Just some test data"}).then(function() { alert("it succeeded!"); }, function() { alert("it failed!"); }); }); }); }); // Add headers to all your calls $app.on("beforeapiget beforeapipost", function(e, request, settings) { request.setRequestHeader("My test header", "My test value"); }); // Run the application $app.run(); }); </script> </body> </html>