Почему self.render () не работает? Фронт не может получить мое сообщение - PullRequest
0 голосов
/ 04 сентября 2018

Все работает нормально, кроме отправки сообщения на фронт в self.render сообщения. Кроме того, код после self.render () также работал. Было ли отключено это соединение? В бэкэнде не было никаких исключений.

class ListsHandler(tornado.web.RequestHandler):
        def get(self):
            print('get')
            agent_id = self.get_argument('agent_id', None)
            answer_sql = 'select id,value from answer_list'
            cursor.execute(answer_sql)
            answer_results = cursor.fetchall()
            question_sql = 'select id,value from question_list'
            cursor.execute(question_sql)
            question_results = cursor.fetchall()
            answer_results=list(answer_results)
            question_results = list(question_results)
            print 'answer:',answer_results
            print 'question',question_results

            self.render("lists.html", agent_id=agent_id, answer_results=answer_results, question_results=question_results)

        def post(self):

            id = self.get_argument('id', None)
            id=int(id)
            agent_id = self.get_argument('agent_id')  
            if id:
                id-=1
                new_id=id-1
                print(agent_id, id)
                cursor.execute('delete from question_list where id=%s' % (id))
                cursor.execute('update question_list set id=id-1 where id>%s' % (id))
                cursor.execute('delete from answer_list where id=%s' % (id))
                cursor.execute('update answer_list set id=id-1 where id>%s' % (id))
                db.commit()
                answer_sql = 'select id,value from answer_list'
                cursor.execute(answer_sql)
                answer_results = cursor.fetchall()
                question_sql = 'select id,value from question_list'
                cursor.execute(question_sql)
                question_results = cursor.fetchall()
                question_dict[agent_id] = question_results
                self.render("lists.html", agent_id=agent_id, answer_results=answer_results,
                            question_results=question_results)

enter image description here

Как видите, GET и self.render в GET такие же, как и в POST. Только это не удалось, странно. И я отправляю 404 на фронт. Браузер получает без каких-либо операций.

enter image description here Что-то не так с моим html?

enter image description here

1 Ответ

0 голосов
/ 05 сентября 2018

Спасибо за помощь от Свена Фестерсена. Ajax не получает мои данные из серверной части рендера. Так что проблом решается легко.

def post(self):
    # id为此条问答的id,是lists中显示的id-1
    id = self.get_argument('id', None)
    agent_id = self.get_argument('agent_id')  # 获取用户场景id
    pn = self.get_argument('pn', 1)
    pn = int(pn)
    if id:
        id = int(id)
        id-=1
        new_id=id-1
        cursor.execute('delete from question_list where id=%s' % (id))
        cursor.execute('update question_list set id=id-1 where id>%s' % (id))
        cursor.execute('delete from answer_list where id=%s' % (id))
        cursor.execute('update answer_list set id=id-1 where id>%s' % (id))
        db.commit()

И html - ключ к этому.

<script>
            $(function(){
                $('#navHeight').height($(document).height()-85);
                $('#table').on('click','tr .del',function(){
                    var id=$(this).attr('data-id');
                    $.ajax({
                        url:'http://10.240.100.38:8888/lists.html?agent_id='+{{agent_id}},
                        type:"POST",
                        data:{_method:"POST",id:id},
                        success : function(data) {
                            location.reload(true);
                        }
                    })
                })
            })
        </script>

Я изменил скрипт, а не использовал функцию рендеринга.

...