首页 > POST 消息到后台一直提示 400 BAD request

POST 消息到后台一直提示 400 BAD request

前端

$.ajax({
    url: "/profile/{{ g.user.user_id }}",
    type: 'POST',
    data: {data: "failed"},
    error: function(e) {
        console.log(e);
    }
})
.done(function (data) {
    console.log(data);
});

后台,为了测试post消息写的

@app.route('/profile/<int:user_id>', methods=['GET', 'POST'])
@login_required
def profile(user_id):
    if request.method == 'POST':
        print request.method
        data = request.form.get('data')
        return data

后台只能收到 GET 消息,无法收到 POST 消息,但是其他页面同样的方式实现的没问题。

实在不知道什么原因,求教!!!


哎,自己挖的坑,果然要自己填;

之前后台为了安全性,开启了CRSF(Cross Site Request Forgery),而该页面中发送请求时并未对此进行验证。

自己SX了,一直检查不出来,一个是开始在手机端,不好调试(暂时推荐:http://jsconsole.com/?%3Alisten),ajax的post错误了没反馈信息,只知道400 ban request,所以一直没找到问题所在。

具体参看:
http://flask-wtf.readthedocs.org/en/latest/csrf.html


大哥,post不是这样传参数的。你这是用get的形式做post,不对。


@app.route('xxx', methods=['GET', 'POST'])


url 改成 "/profile/"+ g.user.user_id
【热门文章】
【热门文章】