通过 ExtJS.request.Ajax POST 从表单提交日期到 Flask API
Submitting date from form via ExtJS.request.Ajax POST to Flask API
为什么椰枣是皇家 PITA:
我有一个带日期选择器的 ExtJS(版本 6.2.0)表单面板:
{
allowBlank:false,
fieldLabel: 'Consent Date',
name: 'consent_date',
emptyText: 'consent_date',
//inputType: 'date',
xtype: 'datefield',
//maxValue: new Date(),
format: 'm/d/Y',
submitFormat: 'm/d/Y'
}
我正在通过 Ext.request.Ajax 调用在我的控制器中提交:
onNewPatientFormSubmit: function () {
var formPanel = this.lookupReference('newPatientForm'),
form = formPanel.getForm(),
url = 'http://127.0.0.1:5000/mrnconsentview/api/create';
if (form.isValid()) {
values = form.getFieldValues(true);
console.log('form');
console.log(form);
console.log(values);
form.reset();
Ext.MessageBox.alert(
'Thank you!',
'Your inquiry has been sent. We will respond as soon as possible.'
);
Ext.Ajax.request({
method: 'POST',
cors: true,
timeout: 6000000, //default is 30 seconds
useDefaultXhrHeader: false,
url: url,
params: values,
headers: {
'Accept': 'application/json'
},
disableCaching: false,
success: function (response) {
json = Ext.decode(response.responseText);
console.log('json');
console.log(json);
console.log(response);
if (response.status === 200) {
console.log(json.items);
}
else {
Ext.MessageBox.alert('Error', response.message);
}
}
});
}
}
问题是,无论我的表单面板中的提交格式如何,它总是呈现提交的 consent_date 格式如下:Fri Jan 02 1995 00:00:00 GMT-0600 (CST)
因此,我的 Ajax 中的 JSON ] 表单提交看起来像:
{mrn: "testing12345", consent_date: Mon Jan 02 1995 00:00:00 GMT-0600 (CST)}
在服务器(Flask,with wtforms)上更糟,它看起来像:1995-01-02T00:00:00
因此,因为它没有将其视为日期时间对象而窒息......来自服务器的响应是
{
"error_details": {
"consent_date": [
"Not a valid datetime value"
]
},
"message": "Validation error"
}
我可以对其进行 post 处理,但我更愿意在不必这样做的情况下获取它。 (我尝试使用 python 日期时间方法进行最小 post 处理,但放弃了...... ala:t = datetime.datetime.strptime(dict(request.form)['consent_date'][0], "%Y-%m-%dT%H:%M:%S.%f")
并收到错误:time data '1995-01-02T00:00:00' does not match format '%Y-%m-%d%H:%M:%S.%f'
)
您应该改用 getValues 表单方法。 getFieldValues
没有考虑 submitFormat
.
getFieldValues: ..This is similar to getValues except that this method
collects type-specific data values (e.g. Date objects for date fields)
while getValues returns only String values for submission.
getValues: ..This is similar to getFieldValues except that this method collects only String values for submission, while
getFieldValues collects type-specific data values (e.g. Date objects
for date fields.)
为什么椰枣是皇家 PITA:
我有一个带日期选择器的 ExtJS(版本 6.2.0)表单面板:
{
allowBlank:false,
fieldLabel: 'Consent Date',
name: 'consent_date',
emptyText: 'consent_date',
//inputType: 'date',
xtype: 'datefield',
//maxValue: new Date(),
format: 'm/d/Y',
submitFormat: 'm/d/Y'
}
我正在通过 Ext.request.Ajax 调用在我的控制器中提交:
onNewPatientFormSubmit: function () {
var formPanel = this.lookupReference('newPatientForm'),
form = formPanel.getForm(),
url = 'http://127.0.0.1:5000/mrnconsentview/api/create';
if (form.isValid()) {
values = form.getFieldValues(true);
console.log('form');
console.log(form);
console.log(values);
form.reset();
Ext.MessageBox.alert(
'Thank you!',
'Your inquiry has been sent. We will respond as soon as possible.'
);
Ext.Ajax.request({
method: 'POST',
cors: true,
timeout: 6000000, //default is 30 seconds
useDefaultXhrHeader: false,
url: url,
params: values,
headers: {
'Accept': 'application/json'
},
disableCaching: false,
success: function (response) {
json = Ext.decode(response.responseText);
console.log('json');
console.log(json);
console.log(response);
if (response.status === 200) {
console.log(json.items);
}
else {
Ext.MessageBox.alert('Error', response.message);
}
}
});
}
}
问题是,无论我的表单面板中的提交格式如何,它总是呈现提交的 consent_date 格式如下:Fri Jan 02 1995 00:00:00 GMT-0600 (CST)
因此,我的 Ajax 中的 JSON ] 表单提交看起来像:
{mrn: "testing12345", consent_date: Mon Jan 02 1995 00:00:00 GMT-0600 (CST)}
在服务器(Flask,with wtforms)上更糟,它看起来像:1995-01-02T00:00:00
因此,因为它没有将其视为日期时间对象而窒息......来自服务器的响应是
{
"error_details": {
"consent_date": [
"Not a valid datetime value"
]
},
"message": "Validation error"
}
我可以对其进行 post 处理,但我更愿意在不必这样做的情况下获取它。 (我尝试使用 python 日期时间方法进行最小 post 处理,但放弃了...... ala:t = datetime.datetime.strptime(dict(request.form)['consent_date'][0], "%Y-%m-%dT%H:%M:%S.%f")
并收到错误:time data '1995-01-02T00:00:00' does not match format '%Y-%m-%d%H:%M:%S.%f'
)
您应该改用 getValues 表单方法。 getFieldValues
没有考虑 submitFormat
.
getFieldValues: ..This is similar to getValues except that this method collects type-specific data values (e.g. Date objects for date fields) while getValues returns only String values for submission.
getValues: ..This is similar to getFieldValues except that this method collects only String values for submission, while getFieldValues collects type-specific data values (e.g. Date objects for date fields.)