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

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

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

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

6249 次点击
所在节点    Flask
12 条回复
HanSonJ
2015 年 11 月 20 日
可能与框架有关,有些框架防止跨域
FrankFang128
2015 年 11 月 20 日
因为后台不接受 post
ryd994
2015 年 11 月 20 日
后台开 debug 看 log
skywatcher
2015 年 11 月 20 日
@FrankFang128 应该不是,另一个页面的 post 正常的
skywatcher
2015 年 11 月 20 日
@HanSonJ 看 url 就知道没有跨域啊
skywatcher
2015 年 11 月 20 日
@ryd994 flask 的后台如何看 log , log 不是自己写的么?目前 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
```
realpg
2015 年 11 月 20 日
CHROME F12
网络
然后发起请求,把返回 400 那个 POST 请求上,右键, SAVE AS HAR 然后贴上来
realpg
2015 年 11 月 20 日
更正 COPY ALL AS HAR 不是 SAVE
zeayes
2015 年 11 月 20 日
login_required 里面的处理有问题。

把进程跑在前台,打开 debug 模式,标准输入会打印每个请求的。
skywatcher
2015 年 11 月 20 日
@realpg 问题已解决,开启 CSRF 而没有在 header 里加相关信息 /(ㄒoㄒ)/~~
skywatcher
2015 年 11 月 20 日
@zeayes 问题已解决
HanSonJ
2015 年 11 月 20 日
@skywatcher 其实我想说的就是楼主说的 CRSF ,昨晚没想到这个英文怎么写 - -

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/237510

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX