Javascript 如何使用ajax处理400个错误请求
我想通过ajax post请求发送参数,但我总是得到状态400。这是jsJavascript 如何使用ajax处理400个错误请求,javascript,jquery,ajax,flask,Javascript,Jquery,Ajax,Flask,我想通过ajax post请求发送参数,但我总是得到状态400。这是js $(document).on('click','#create-chart-button', function(e){ e.preventDefault(); var fields = {}; $('.can-drop').each(function(item){ fields[item] = ($(this)[0].innerText); }) ; console
$(document).on('click','#create-chart-button', function(e){
e.preventDefault();
var fields = {};
$('.can-drop').each(function(item){
fields[item] = ($(this)[0].innerText);
}) ;
console.log(fields);
$.ajax({
type:'POST',
url: '/create/api/',
data: {
"fields": fields,
},
cache: true,
success:function(data){
console.log(data);
}
})
});
数据取自用户选择的可拖动对象。这是我在请求对象中获得的参数示例:
fields[0]:"name"
fields[1]:"jobtime"
fields[2]:"id"
我用的是烧瓶,这是我的路线:
@app.route('/create/api/', methods=['POST'])
def populate_chart():
if request.method == 'POST':
fields = request.form['fields']
print(fields)
pass
我不想用表格。调试时,ajax数据以request.form的形式显示(即使我没有使用该表单)。但我也犯了同样的错误。
我尝试使用JSON.stringfy,但似乎没有任何变化。
我将继续尝试解决方案,但每次使用ajax和post时,我都会遇到这个错误,所以我想知道错误在哪里。谢谢
更新
在调试时,我看到:
def __getitem__(self, key):
"""Return the first data value for this key;
raises KeyError if not found.
:param key: The key to be looked up.
:raise KeyError: if the key does not exist.
"""
if key in self:
lst = dict.__getitem__(self, key)
if len(lst) > 0:
return lst[0]
raise exceptions.BadRequestKeyError(key)
我的数据是:
ImmutableMultiDict([('fields[0]', 'callid'), ('fields[1]', 'jobtime')])
引发此异常的原因是,密钥为“fields”,但传递的数据中没有与“fields”匹配的密钥。所以我将以另一种方式传递数据(我猜)