Javascript 跨域ajax post中回调函数未定义
我试图向我的flask应用程序发出ajax post请求,并在回调函数中得到答案。不幸的是,我得到了未定义的值,而不是json 非常感谢您的帮助 我的服务器端代码:Javascript 跨域ajax post中回调函数未定义,javascript,python,ajax,flask,Javascript,Python,Ajax,Flask,我试图向我的flask应用程序发出ajax post请求,并在回调函数中得到答案。不幸的是,我得到了未定义的值,而不是json 非常感谢您的帮助 我的服务器端代码: application = Flask(__name__) CORS(application) ... @application.route('/get_forecast', methods=['POST', 'GET']) def get_forecast(): if request.method == 'POST':
application = Flask(__name__)
CORS(application)
...
@application.route('/get_forecast', methods=['POST', 'GET'])
def get_forecast():
if request.method == 'POST':
print('in post')
predictions = {}
predictions['data'] = calc_forecast(request.get_json()["data"])
print(predictions)
return jsonify(predictions)
$.ajax({
type: "POST",
url: "http://localhost:5000/get_forecast",
data: JSON.stringify(markers),
dataType: 'json',
crossDomain: true,
contentType: "application/json",
done: function(resp, status){
console.log('done')
console.log(resp)
console.log(status)
console.log('=====')
}(),
fail: function(xhr, ajaxOptions, thrownError){
console.log('fail');
console.log(thrownError)
}()
});
我的客户端代码:
application = Flask(__name__)
CORS(application)
...
@application.route('/get_forecast', methods=['POST', 'GET'])
def get_forecast():
if request.method == 'POST':
print('in post')
predictions = {}
predictions['data'] = calc_forecast(request.get_json()["data"])
print(predictions)
return jsonify(predictions)
$.ajax({
type: "POST",
url: "http://localhost:5000/get_forecast",
data: JSON.stringify(markers),
dataType: 'json',
crossDomain: true,
contentType: "application/json",
done: function(resp, status){
console.log('done')
console.log(resp)
console.log(status)
console.log('=====')
}(),
fail: function(xhr, ajaxOptions, thrownError){
console.log('fail');
console.log(thrownError)
}()
});
这是我在烧瓶终端得到的:
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [02/Dec/2018 12:59:53] "OPTIONS /get_forecast HTTP/1.1" 200 -
in post
{'data': [10.15451836569513, 56.578480707072714, 12.435548873275337]}
127.0.0.1 - - [02/Dec/2018 12:59:53] "POST /get_forecast HTTP/1.1" 200 -
以下是我在chrome控制台中得到的信息:
demo.html:228 done
demo.html:229 undefined
demo.html:230 undefined
demo.html:231 =====
demo.html:234 fail
demo.html:235 undefined
如果您仔细查看这些示例,它们看起来与您对
$.ajax
的调用有很大不同。我调整了您的代码以更好地类似于示例。也许这有帮助
$.ajax({
type: "POST",
url: "http://localhost:5000/get_forecast",
data: JSON.stringify(markers),
dataType: 'json',
crossDomain: true,
contentType: "application/json"
})
.done(function (data, textStatus, jqXHR) {
console.log('done');
console.log(data);
console.log(textStatus);
console.log('=====');
})
.fail(function (jqXHR, textStatus, errorThrown) {
console.log('fail');
console.log(textStatus);
});
我想,您正在寻找成功
和错误
设置
更新:是的,我很确定,您的设置
完成
和失败
是错误的。您想使用成功
和错误
还有,看看
这意味着,如果要将所有内容都放入$的设置中,代码也可以是这样。ajax
调用:
$.ajax({
type: "POST",
url: "http://localhost:5000/get_forecast",
data: JSON.stringify(markers),
dataType: 'json',
crossDomain: true,
contentType: "application/json",
success: function (data, textStatus, jqXHR) {
console.log('done');
console.log(data);
console.log(textStatus);
console.log('=====');
},
error: function (jqXHR, textStatus, errorThrown ) {
console.log('fail');
console.log(errorThrown);
}
});