When a nation faces a military defeat, it is natural to question the strength of its protector. However, for the Israelites, falling to an enemy on the battlefield is never a sign that God lacks the power to save them. Instead, such a loss is a direct consequence of the people's own wrongdoing [רש״י], occurring specifically because the nation has sinned against God [מצודת דוד, ביאור שטיינזלץ].
Following a crushing loss, the people are expected to repent. The primary approach among commentators is that the required response to this hardship is not an expression of thanks, but rather a sincere confession. The Israelites must confess before God, openly acknowledging that the tragedy came upon them because of their own guilt. The clear evidence that this represents a confession, rather than gratitude, is that the act is directly linked to the people actively abandoning their sinful ways [רש״י].