0

Why isn't the callback working, the response voice I hear is what i just spoke to it but the program must have listened to what I have said and responded with the call back I assigned to it. The problem i'm guessing is on the postVoice function. The message is properly being recorded on terminal but the code is not interpreting the message to process a callback response. As a side note the "say($("#stuff").text())" line is the one that is working and giving the response but the "postVoice($("#stuff").text())" must have worked the same way as the "say($("#stuff").text())" but it is not.

function say(text) {
    responsiveVoice.speak(text, "UK English Male")
}

function listen() {
    var recognition = new webkitSpeechRecognition();
    recognition.interimResults = true;
    recognition.onresult = function(event) { 
        if (event.results.length > 0) {
            $("#stuff").text(event.results[0][0].transcript)
        }
    }
    recognition.onend = function(event) {
        //say($("#stuff").text())
        postVoice($("#stuff").text())
    }
    recognition.start();
}

function postVoice(message){
    $.ajax({
        type: "POST",
        url: "http://localhost:8000/voice",
        data: {
            message: message
        },
        success: function(data) {
            $("#stuff").text(data.message)
            say(data.message)
        }
    })
}

server side

var express = require('express')
var app = express()

var path = require('path')

var bodyParser = require('body-parser')

app.use(express.static(path.join(__dirname,'../client')))
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(bodyParser.text())



app.post('/voice', function(req, res) {
    getResponse(req.body.message, function(response){
        res.json({
            message: response
        })
    })
})

function getResponse(message, callback){
    if(message == "hello"){
        callback("hey there")
    } else if (message == "goodbye"){
        callback("bye-bye")
    } else {
        callback(message)
    }
}


app.listen(8000, function(){
    console.log("Listening on Port 8000... " + path.join(__dirname, '../client'))
})

  • check this answer: https://stackoverflow.com/a/39154125/8945943 – nassim miled Jul 01 '20 at 23:13
  • 1
    If your front-end and back-end are running from the same service, use an absolute path URL instead of a full one, ie `url: '/voice'` – Phil Jul 01 '20 at 23:14
  • The issue is that you're making a cross-domain request (`http://127.0.0.1:8000 => http://localhost:8000`) when you don't have to – Phil Jul 02 '20 at 00:06
  • Have you tried debugging the value of `message` in `getResponse()`? `console.log('getResponse', message)` should let you know what's going on – Phil Jul 02 '20 at 00:45

0 Answers0