1

当我分别运行 JavaScript 和 VXML 代码时,它们运行良好。但是当我尝试通过 VXML 代码调用 JavaScript 函数时,它不起作用。请在下面找到 VXML 代码。请帮我解决问题。

<vxml version="2.1">
    <form id="selectSong">
        <block>
            <script>
                <![CDATA[
                function selectSong()
                {
                    var number = 1;
                    var currSong = '../songs/buddhist/0001.wav';
                    var fields = currSong.split('/');
                    var song = fields[3];

                    var fields_song = song.split('.');
                    var num = fields_song[0];

                    if (number == 1) {
                        num++;
                    } else if (number == 2) {
                        num--;
                    }

                    if (num > 9 && num < 99)
                    {
                        return "'../songs/buddhist/00" + num + ".wav'";
                    } else if (num < 9 && num > 0) {
                        return "'../songs/buddhist/000" + num + ".wav'";
                    } else if (num > 99 && num > 999) {
                        return "'../songs/buddhist/0" + num + ".wav'";
                    } else {
                        return "'../songs/buddhist/" + num + ".wav'";
                    }
                }
                ]]>
            </script>
        </block>
        <block>
            <prompt>
                <audio expr="'../Prompts/Press1.wav'" />
                <audio expr="'../Prompts/SelectLangPreference.wav'" />                
            </prompt>
        </block>
        <field name="getNum" >
            <grammar xml:lang="en-US" root = "MYRULE" mode="dtmf">
                <rule id="MYRULE" scope = "public">
                    <one-of>
                        <item> 1 </item>
                        <item> 2 </item>
                        <item> 3 </item>
                        <item> 4 </item>
                    </one-of>
                </rule>
            </grammar>
            <filled namelist="getNum" mode="all">
                <if cond="getNum == 1">
                    <prompt>
                        <audio expr="selectSong()" />
                    </prompt>
                </if>
            </filled>
        </field>
    </form>‍‍‍‍‍‍‍
</vxml>
4

1 回答 1

1

“块”元素使匿名范围。因为您的变量无法到达您的“字段”元素。也许你有两个解决方案。

  1. 删除“块”元素外部“脚本”元素。
  2. 将“脚本”元素移动到外部“表单”元素。
于 2013-12-16T01:29:53.260 回答