Discuss / Python / 为什么该ajax只拿到一个响应而不是两个?

为什么该ajax只拿到一个响应而不是两个?

Topic source

alienation

#1 Created at ... [Delete] [Delete and Lock User]

如题,manage_blog_edit.html这个网页的脚本里面postjson执行后访问的是api/blogs/响应函数,这个函数最后返回了blog

虽然postjson最后重定向到了manage/blogs,但是之前返回的blog为什么没有返回为一个以json编码的网页???

var
    ID = '{{ id }}',
    action = '{{ action }}',
    vm;

function initVM(blog) {
     vm = new Vue({
        el: '#vm',
        data: blog,
        methods: {
            submit: function (event) {
                event.preventDefault();
                var $form = $('#vm').find('form');
                $form.postJSON(action, this.$data, function (err, r) {
                    if (err) {
                        $form.showFormError(err);
                    }
                    else {
                        return location.assign('/manage/blogs');
                    }
                });
            }
        }
    });
    $('#vm').show();
}
@post('/api/blogs')
async def api_create_blog(request, *, name, summary, content):
    # 检查文章三要素是否具备
    check_admin(request)
    if not name or not name.strip():
        raise APIValueError('name', 'name cannot be empty.')
    if not summary or not summary.strip():
        raise APIValueError('summary', 'summary cannot be empty.')
    if not content or not content.strip():
        raise APIValueError('content', 'content cannot be empty.')
    # 根据post内容创建blog对象,并保存到数据库
    blog = Blog(user_id=request.__user__.id, user_name=request.__user__.name, user_image=request.__user__.image, name=name.strip(), summary=summary.strip(), content=content.strip())
    await blog.save()
    # 只是为了符合响应函数对象的call方法结构,必须有一个回执
    return blog

  • 1

Reply