Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.52 KB | None | 0 0
  1. <html>
  2. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  3. <body>
  4. </br>
  5. <div>
  6.     <input id="button" type="button" value="Load Activity" onclick="loadActivity($('#activityId').val())"/>
  7.     <input id="activityId" type="text" value="20202"/><input id="token" type="text" value=""/>
  8. </div>
  9. </br>
  10. <textarea rows="100" cols="200"></textarea>
  11.  
  12. </body>
  13.  
  14. <script>
  15.     $(document).ready(function() {
  16.         authenticate();
  17.     });
  18.     function authenticate() {
  19.         //Authentication endpoint
  20.         var url = "http://localhost/rest/security/user/";
  21.         var authRequest = {};
  22.         authRequest.username = 'atl@amp.org';
  23.         authRequest.password = 'a9993e364706816aba3e25717850c26c9cd0d89d';
  24.         authRequest.workspaceId = 117;
  25.         var client = new XMLHttpRequest();
  26.         client.open("POST", url, true);
  27.         client.setRequestHeader("Content-Type", "application/json");
  28.         client.setRequestHeader("Acept", "application/json");
  29.         client.onload= function (e){
  30.             if (client.status == 200) {
  31.                 $('#token').val(JSON.parse(client.responseText).token);
  32.             } else{
  33.                 alert("The request did not succeed!\n\nThe response status was: " + client.status + " " + client.statusText + ".");
  34.                 alert("The error message was " + client.responseText );
  35.             }
  36.         };
  37.         client.onerror = function (e) {
  38.             console.error(xhr.statusText);
  39.         };
  40.         client.send(JSON.stringify(authRequest));
  41.     }
  42.  
  43.     function loadActivity(activityId) {
  44.         var url = "http://localhost:8080/rest/activity/projects/" + activityId;
  45.         var client = new XMLHttpRequest();
  46.         client.open("GET", url, true);
  47.         client.setRequestHeader("Content-Type", "application/json");
  48.         client.setRequestHeader("X-Auth-Token", $('#token').val());
  49.         client.onload = function(e) {
  50.             if (client.status == 200) {
  51.                 // we parse the response object in case we need to process it as a JavaScript object
  52.                 jsonobj = JSON.parse(client.responseText);
  53.                 $('textarea').text(JSON.stringify(jsonobj, null, '\t'));
  54.             } else {
  55.                 //In case client.status is not 2xx then we need to properly handle errors.
  56.                 alert("The request did not succeed!\n\nThe response status was: " + client.status + " " + client.statusText + ".");
  57.             }
  58.             ;
  59.         };
  60.         client.send();
  61.     }
  62. </script>
  63. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement