Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The Api module is designed to handle all interactions with the server
- var Api = (function() {
- var requestPayload;
- var responsePayload;
- var messageEndpoint = '/api/message';
- // Publicly accessible methods defined
- return {
- sendRequest: sendRequest,
- // The request/response getters/setters are defined here to prevent internal methods
- // from calling the methods without any of the callbacks that are added elsewhere.
- getRequestPayload: function() {
- return requestPayload;
- },
- setRequestPayload: function(newPayloadStr) {
- requestPayload = JSON.parse(newPayloadStr);
- },
- getResponsePayload: function() {
- return responsePayload;
- },
- setResponsePayload: function(newPayloadStr) {
- responsePayload = JSON.parse(newPayloadStr);
- }
- };
- // Send a message request to the server
- function sendRequest(text, context) {
- // Build request payload
- var payloadToWatson = {};
- if (text) {
- payloadToWatson.input = {
- text: text
- };
- }
- if (context) {
- payloadToWatson.context = context;
- }
- // Built http request
- var http = new XMLHttpRequest();
- http.open('POST', messageEndpoint, true);
- http.setRequestHeader('Content-type', 'application/json');
- http.onreadystatechange = function() {
- if (http.readyState === 4 && http.status === 200 && http.responseText) {
- Api.setResponsePayload(http.responseText);
- //alert(http.responseText); <-------- Aqui eu pego a resposta vindo do watson...
- var obj = JSON.parse(http.responseText);
- alert(JSON.stringify(obj["context"]["tutorial"]));
- if(JSON.stringify(obj["context"]["tutorial"]) == JSON.stringify("instalar_impressora"))
- {
- //QUANDO CHEGAR AQUI... É PRA MUDAR A IMG LA........
- //document.getElementById('myImage').src='pic_bulboff.gif
- //$(".conteudo").funcao
- var el = document.getElementById( 'watson-column' );
- alert( el.outerHTML );
- }
- else{
- alert("0");
- }
- }
- };
- var params = JSON.stringify(payloadToWatson);
- // Stored in variable (publicly visible through Api.getRequestPayload)
- // to be used throughout the application
- if (Object.getOwnPropertyNames(payloadToWatson).length !== 0) {
- Api.setRequestPayload(params);
- }
- // Send request
- http.send(params);
- }
- }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement