我正在做一些Ajax第一次...代碼:即使在捕獲失敗時仍存在控制檯錯誤?
jQuery的
form_ajax_promise = $.ajax({
type : "POST",
url : '/orders/create_or_update',
dataType: 'json',
contentType: 'application/json',
data : JSON.stringify(params)
})
form_ajax_promise.then(
function(response) {
formSuccess(response)
},
function(response) {
formFailure(response)
}
)
控制器
def create_or_update
if @object.save
# corresponds with formSuccess
render json: {"id" => @order.id}, status: 200
else
# corresponds with formFailure
render json: {"errors"=> @order.errors.full_messages}, status: 400
end
end
的success
路徑效果很好。在測試failure
路線,假設formFailure
只是一個簡單的功能...
function formFailure(response){
console.log("successfully inside of formFailure")
}
什麼我注意到正在發生的事情是,console
顯示相應的日誌消息上面,同時也說明我的錯誤:
Failed to load resource: the server responded with a status of 400 (Bad Request)
這個錯誤是否應該發生?我覺得自從我在$.then
中提供了足夠的fail
它不應該?
編輯
道歉的混亂,這不是多的情況下渲染/重定向,我只是懶惰和切割出的其他代碼,因爲我只是想描述失敗的行爲。我的錯。代碼在上面編輯。
你正在做一個'status:400'的渲染,所以這是正確的行爲。 – Sajan
兩個問題(1)哪個版本的jQuery? (2)'form_ajax_promise'是否進一步處理? –
1)jQuery 2.2.4,2)它不被進一步處理 – james