The men of the ambush also came out of the city against them, so that they were caught in the middle, with Israelites on both sides. Israel cut them down, leaving them neither survivors nor fugitives.1
References for Joshua 8:22
But they took the king of Ai alive2 and brought him to Joshua.
References for Joshua 8:23
When Israel had finished killing all the men of Ai in the fields and in the desert where they had chased them, and when every one of them had been put to the sword, all the Israelites returned to Ai and killed those who were in it.