简体   繁体   English

Flask 服务器从 JS rest 调用返回 400 错误代码

[英]Flask server returning 400 error code from JS rest call

I'm calling my Flask view function (API) via Javascript (FETCH).我正在通过 Javascript (FETCH) 调用我的 Flask 视图 function (API)。

I'm having success with the GET method, but when using the PATCH method I'm receiving a 400 error code (Failed to load resource: the server responded with a status of 400 (BAD REQUEST)).我使用 GET 方法取得了成功,但是在使用 PATCH 方法时,我收到了 400 错误代码(加载资源失败:服务器响应状态为 400(错误请求))。

I reviewed the url, seems ok and the ID is being passed as an integer, so no clue on why it's giving this error message.我查看了 url,似乎没问题,并且 ID 作为 integer 传递,所以不知道为什么它会给出此错误消息。

This function (load_follow_link) is fetching via GET and updating my "Follow" to "Unfollow" tag (no errors here):此 function (load_follow_link) 正在通过 GET 获取并将我的“关注”更新为“取消关注”标签(此处没有错误):

function load_follow_link(id) {
    apply_csrf_token();

    fetch(`/follow_unfollow/${id}`)
    .then(response => response.json())
    .then(data => {
        if (data.is_following)
            document.getElementById('follow-unfollow-btn').innerHTML = 'Unfollow';
        else 
            document.getElementById('follow-unfollow-btn').innerHTML = 'Follow';
        
        document.getElementById('followers-count').innerHTML = data.followers_count;
        document.getElementById('following-count').innerHTML = data.following_count;
    });
}

This is the PATCH function (follow_unfollow) triggering the error message.这是触发错误消息的 PATCH function (follow_unfollow)。 It is supposed to call the view function and update the DB:它应该调用视图 function 并更新数据库:

function follow_unfollow(id) {
    apply_csrf_token();
    
    fetch(`/follow_unfollow/${id}`, {
        method: 'PATCH'
    })
    .then(() => {
        load_follow_link(id);
    });
}

view function (doesn't get executed when request method is PATCH)查看 function(请求方法为 PATCH 时不执行)

@app.route('/follow_unfollow/<int:tutor_id>', methods=['GET','PATCH'])
@login_required
def follow_unfollow(tutor_id):
    """ GET: returns if user is following the tutor, followers and following total
        PUT: follow or unfollow
    """
    user = Users.query.get(current_user.id)
    try:
        tutor = Tutors.query.get(tutor_id)
    except NoResultFound:
        return JsonResponse({"error": "Tutor not registered"}, status=401)

    following_count = user.following_total()
    followers_count = tutor.followers_total()
    is_following = user.is_following(tutor)

    if request.method == 'GET':
        return jsonify(is_following=is_following, followers_count=followers_count,
            following_count=following_count)
    
    elif request.method == 'PATCH':
        if is_following:
            user.unfollow(tutor)
        else:
            user.follow(tutor)
        db.session.commit()
        return success.return_response(message='Successfully Completed', status=204)
    else:
        return jsonify(error="GET or PUT request required", status=400)

I appreciate the help我很感激帮助

Could be you are not returning a valid response.可能是您没有返回有效的响应。 Where is success defined in success.return_response ? success.return_response中定义的success在哪里?

Why not just return jsonify(...) ?为什么不直接return jsonify(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM