【发布时间】:2016-02-28 14:08:00
【问题描述】:
我使用 scala.js (0.6.5) 和 scala-js-dom (0.8.2),当我收到错误状态(此处为 409)时,我有一些带有 ajax.post 的奇怪 pb。
浏览器控制台显示一条错误消息,但从我的 scala 代码中,我无法访问状态代码和返回的消息。
这是我用于发送 POST 的代码:
val request = Ajax.post(
url,
data = postData,
headers = bsHeaders)
request.map(xhr => {
log.debug("response text: " + xhr.responseText)
if (xhr.status == 201) {
try {
val loc = xhr.getResponseHeader("Location")
if(loc == locHeaderResp) {
loc
} else {
log.error(s"Location header invalid: ${loc}")
}
} catch {
case e:Exception => {
log.error("Couldn't read 'Location' header " + e.getMessage)
log.debug("List of headers: " + xhr.getAllResponseHeaders())
""
}
}
} else if (xhr.status == 409) {
log.error("" + xhr.responseText)
log.error(s"${xhr.responseText}")
} else {
log.error(s"Request failed with response code ${xhr.status}")
log.error(s"${xhr.responseText}")
}
})
当状态为201时,效果很好。
在我的情况下,当我发送的数据已经存在时,我应该得到一个 409 错误代码,以及一些消息状态。而从浏览器调试工具来看确实如此。
我希望在执行“request.map”时能够管理错误情况,但是当返回错误代码时,此代码不会执行。
那么如何管理 POST 消息的错误呢?
【问题讨论】:
标签: scala.js