Advertisement
Guest User

s

a guest
Mar 26th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // The Api module is designed to handle all interactions with the server
  2.  
  3. var Api = (function() {
  4.   var requestPayload;
  5.   var responsePayload;
  6.   var messageEndpoint = '/api/message';
  7.  
  8.   // Publicly accessible methods defined
  9.   return {
  10.     sendRequest: sendRequest,
  11.  
  12.     // The request/response getters/setters are defined here to prevent internal methods
  13.     // from calling the methods without any of the callbacks that are added elsewhere.
  14.     getRequestPayload: function() {
  15.       return requestPayload;
  16.     },
  17.     setRequestPayload: function(newPayloadStr) {
  18.       requestPayload = JSON.parse(newPayloadStr);
  19.     },
  20.     getResponsePayload: function() {
  21.       return responsePayload;
  22.     },
  23.     setResponsePayload: function(newPayloadStr) {
  24.       responsePayload = JSON.parse(newPayloadStr);
  25.     }
  26.   };
  27.  
  28.   // Send a message request to the server
  29.   function sendRequest(text, context) {
  30.     // Build request payload
  31.     var payloadToWatson = {};
  32.     if (text) {
  33.       payloadToWatson.input = {
  34.         text: text
  35.       };
  36.     }
  37.     if (context) {
  38.       payloadToWatson.context = context;
  39.     }
  40.  
  41.     // Built http request
  42.     var http = new XMLHttpRequest();
  43.     http.open('POST', messageEndpoint, true);
  44.     http.setRequestHeader('Content-type', 'application/json');
  45.     http.onreadystatechange = function() {
  46.       if (http.readyState === 4 && http.status === 200 && http.responseText) {
  47.          
  48.         Api.setResponsePayload(http.responseText);
  49.          //alert(http.responseText); <-------- Aqui eu pego a resposta vindo do watson...
  50.           var obj = JSON.parse(http.responseText);
  51.           alert(JSON.stringify(obj["context"]["tutorial"]));
  52.           if(JSON.stringify(obj["context"]["tutorial"]) == JSON.stringify("instalar_impressora"))
  53.           {
  54.               //QUANDO CHEGAR AQUI... É PRA MUDAR A IMG LA........
  55.               //document.getElementById('myImage').src='pic_bulboff.gif
  56.               //$(".conteudo").funcao
  57.               var el = document.getElementById( 'watson-column' );
  58.               alert( el.outerHTML );
  59.           }
  60.           else{
  61.               alert("0");
  62.           }
  63.       }
  64.     };
  65.     var params = JSON.stringify(payloadToWatson);
  66.    
  67.     // Stored in variable (publicly visible through Api.getRequestPayload)
  68.     // to be used throughout the application
  69.     if (Object.getOwnPropertyNames(payloadToWatson).length !== 0) {
  70.       Api.setRequestPayload(params);
  71.     }
  72.  
  73.     // Send request
  74.     http.send(params);
  75.   }
  76. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement