【问题标题】:401 Unauthorized when moving web2py from localhost to PythonAnywhere将 web2py 从 localhost 移动到 PythonAnywhere 时出现 401 Unauthorized
【发布时间】:2015-05-23 00:21:45
【问题描述】:

在我的应用中,我有一个用户个人资料页面,用户可以在其中上传个人资料图片的图片。此功能在 localhost 上运行时按预期工作,但由于迁移到 PythonAnywhere 后出现 401 Unauthorized 失败。

@auth.requires_login() 装饰的函数上,POST 是否有任何原因会失败并出现 401,尽管已登录?

ajax 调用:

$('#image_form').submit(function(event){
    event.preventDefault(); 
    var up = new FormData(this);
   $.ajax({
        url: '/dids/default/update_profile',
        data: up,
        processData: false,
        contentType: false,
        type: 'POST',
        success: function(data){
            console.log(data);
        }
    });
}); 

表格:

<div id=edit_div type="hidden">
    <form id="image_form" type="hidden" enctype="multipart/form-data" action="update_profile" method="post">
        <input id="image" name="image" type="file"/>
        <input name="is_img" type="hidden" value="True"/>
    </form>
</div>

控制器功能:

@auth.requires_login()
def update_profile():
    data = request.vars
    user_id = auth.user_id
    db(db.users.user_id == user_id).update(profile_img=data['image'])
    return 

注意,我也尝试在.update() 之前使用db.users.profile_img.store(),结果相同

表定义:

db.define_table('users',
    Field('user_id',),
    Field('username'),
    Field('first_name'),
    Field('last_name'),
    Field('profile_img', 'upload'),  # image field
    Field('about', 'text'),
    Field('email'),
    Field('dids', 'reference dids'),
    Field('feed'))

注意:这部分网站不是我的代码,而是一个队友。不幸的是,当它坏了时,我现在要修复它。我改变很多,但我想尽可能少地改变,以防止破坏他所做的任何其他事情。

【问题讨论】:

  • 快速检查您的 ajax 帖子,您是否包含 csrf 令牌?

标签: jquery ajax web2py pythonanywhere


【解决方案1】:

你能发布完整的跟踪吗? (也许 routes.py 出了点问题/不同)

【讨论】:

    猜你喜欢
    • 2014-04-15
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    • 2016-02-02
    • 2021-12-11
    • 2023-03-23
    • 2014-04-06
    • 1970-01-01
    相关资源
    最近更新 更多