Javascript 从POST返回数据
我在运行一台烧瓶服务器。JS文件通过POST请求向服务器发送数据。这是服务器代码:Javascript 从POST返回数据,javascript,jquery,flask,Javascript,Jquery,Flask,我在运行一台烧瓶服务器。JS文件通过POST请求向服务器发送数据。这是服务器代码: @app.route('/approve', methods=['POST']) def approve(): try: assignmentId = request.form['assignmentId'] response = client.approve_assignment( AssignmentId=assignmentId
@app.route('/approve', methods=['POST'])
def approve():
try:
assignmentId = request.form['assignmentId']
response = client.approve_assignment(
AssignmentId=assignmentId
)
return make_response(jsonify(response), response['ResponseMetadata']['HTTPStatusCode'])
except ClientError as e:
return make_response(jsonify(e.response), e.response['ResponseMetadata']['HTTPStatusCode'])
在JS中,我希望有一个警报来通知操作是否成功;如果失败,那么具体的错误是什么。但是,返回的数据是空的,我无法检查它以通知用户特定的错误
$.post("http://cronus.eecs.northwestern.edu/mturk/approve",
{assignmentId: assignmentId,
OverrideRejection: false},
function(data, status) {
console.log(data);
console.log(status);
if (status == 200) {
alert("Approved successfully");
} else {
alert("Error");
}
});
我怎样才能解决这个问题?数据是否为空,因为我没有在Flask代码中使用正确的响应对象?您应该使用它
$.post( "example.php", function() {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
.fail(function(error) {
alert( `error ${error}` );
})
.always(function() {
alert( "finished" );
});
你应该用这个
$.post( "example.php", function() {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
.fail(function(error) {
alert( `error ${error}` );
})
.always(function() {
alert( "finished" );
});