Im使用sweetalert2
和sweetalert2-react-content
。我的inputValidator
工作正常,向用户显示了错误。如何在有AJAX请求的preConfirm
中执行相同的操作?
MySwal.fire({
title: 'title',
input: 'text',
html: this.renderInfoText(),
showLoaderOnConfirm: true,
inputValidator: (value) => {
return new Promise((resolve) => {
if (this.validate(value)) {
resolve()
} else {
resolve('check formatting...')
}
})
},
preConfirm: (pno) => {
return axios.post('/some/route', { pno })
.then(response => {
// ...
})
.catch(error => {
// show an error message to the user from here?
});
}
})
这实际上记录在examples下的AJAX request example
中。这是我的解决方案:
MySwal.fire({
title: 'title',
input: 'text',
html: this.renderInfoText(),
showLoaderOnConfirm: true,
inputValidator: (value) => {
return new Promise((resolve) => {
if (this.validate(value)) {
resolve()
} else {
resolve('check formatting...')
}
})
},
preConfirm: (pno) => {
return axios.post('/some/route', { pno })
.then(response => {
// ...
})
.catch(error => {
if (error.response.status === 404) {
MySwal.showValidationMessage(error.response.data.message)
}
});
}
})
here在某些示例中,他们正在使用访存,请尝试使用它
MySwal.fire({
title: 'title',
input: 'text',
html: this.renderInfoText(),
showLoaderOnConfirm: true,
inputValidator: (value) => {
return new Promise((resolve) => {
if (this.validate(value)) {
resolve()
} else {
resolve('check formatting...')
}
})
},
preConfirm: (pno) => {
return fetch('/some/route', {
method: 'POST',
body: JSON.stringify(pno),
headers: {
'Content-Type': 'application/json'
})
.then(response => {
// ...
})
.catch(error => {
// show an error message to the user from here?
});
}
})