Вызов внешнего веб-сервиса в библиотеке JS - PullRequest
0 голосов
/ 03 апреля 2019

Я пытаюсь создать виджет чата, используя библиотеку JS симулятора чата, и попытался использовать его с веб-сервисом. Один из следующих вопросов:

  1. Позвоните во внешнюю веб-службу, когда пользователь достигнет последнего фиктивного сообщения чата, которое «введите вашу проблему»
  2. Передать собранные данные в API веб-службы
  3. Ответ с реальным сообщением чата от внешнего веб-сервиса после последнего фиктивного сообщения чата

Я не могу получить данные ответа из / в веб-службу, когда пользователь достиг «Введите вашу проблему», в настоящее время происходит перезагрузка чата, когда он достигает «Введите вашу проблему», и все было очищено .

Пожалуйста, помогите мне.

JS:

var chatInfo = {chatInfo: [...]};

        alert ('aaa');
        function getinfo(convState, ready) {
            alert ('bbb');
            var chatMsg = $('#chatConcern').val();
                if(chatMsg>=0) {
                    alert ('ccc');
                    jQuery.ajax({
                        url: 'https://example.webservice/',
                        type: "POST",
                        dataType: "json",
                        contentType: 'application/json; charset=utf-8',
                        data: JSON.stringify(chatInfo),
                        success: function (response) {
                            console.log("Thanks!");
                        },
                        error: function (response) { alert(response.d); }

                    });
                } else {
                    convState.current.next = false;
                }
        }

        jQuery(function($){
            var count = 0;
            var original = false;
            var convForm = $('#chat').convform({
                eventList:{
                    onInputSubmit: function(convState, ready) {
                        console.log('input is being submitted...');
                //here you send the response to your API, get the results and build the next question
                //when ready, call 'ready' callback (passed as the second parameter)
                if(convState.current.answer.value ==='end') {
                    convState.current.next = original;
                    //emulating random response time (100-600ms)
                    setTimeout(ready, Math.random()*500+100);
                } else {

                    setTimeout(ready, Math.random()*500+100);
                }
                count++;

                /* function getinfo(stateWrapper, ready) {
                    window.open("https://google.com");
                    ready();
                } */
                    }
                }
            });
        });

HTML:

<section id="demo">
        <div class="vertical-align">
            <div class="container">
                <div class="row">
                    <div class="col-sm-6 col-sm-offset-3 col-xs-offset-0">
                        <div class="card no-border">
                            <div id="chat">
                                <form action="" method="GET" class="hidden">
                                    <select data-conv-question="Hello! This is an example use of the plugin to dynamically generate questions (like using an API). This is the only question that was written on the initial HTML. To end the loop, select END." name="first-question">
                                        <option value="understood">Understood</option>
                                        <option value="okay">Okay, captain!</option>
                                    </select>

                                    <div data-conv-case="understood">
                                        <input type="text" name="fullName" data-conv-question="What is your full name?">
                                        <input type="email" data-pattern="^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$" name="email" data-conv-question="What is your e-mail address?">
                                        <input type="concern" name="cs" data-conv-question="Type your concern">
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
</section>
...