Rex Ryan's homecoming was a victorious one -- barely.

The Buffalo Bills raced out to a 19-point third quarter lead on Thursday before withstanding a furious rally by the New York Jets to hang on for a 22-17 victory at MetLife Stadium.

The Bills won their second in a row to improve to 5-4 and moved into the thick of the AFC wild card picture in their first year under Ryan, who coached the Jets from 2009-14.

The Jets (5-4) have lost three of four.

The Bills led 12-3 at the half and extended the lead to 22-3 in the first seven minutes of the third quarter when quarterback Tyrod Taylor threw a 26-yard touchdown pass to running back Karlos Williams and kicker Dan Carpenter booted a 29-yard field goal.

But the Jets pulled within five on a pair of touchdown passes by quarterback Ryan Fitzpatrick -- a 14-yarder to Brandon Marshall late in the third quarter and a 31-yarder to Eric Decker midway through the fourth quarter.

The Bills went three-and-out on their next possession and prepared to punt from their own 28-yard-line. But punter Colton Schmidt dropped the snap and was swallowed up by several Jets as he recovered it at the Bills' 13-yard-line with 5:12 left.

But the Jets could not take advantage.

Running back Chris Ivory gained eight yards on first down before losing a yard on second down. A short pass to Decker lost a yard on third down and Fitzpatrick's fourth down pass into the end zone sailed over the head of tight end Kellen Davis.

The Bills punted with less than a minute left, but Fitzpatrick's next pass was picked off. Ryan responded by pumping his fist so hard his hat fell off.

Running back LeSean McCoy gained 112 yards on 19 carries for the Bills.

Fitzpatrick was 15-of-34 for 193 yards, two touchdowns and two interceptions.

Ryan jogged out of the tunnel at to a chorus of boos from a sparse, late-arriving crowd just priot to kickoff.

The boos grew a little louder a few minutes later, when the captains -- including Bills linebacker IK Enemkpali, who was released by the Jets after slugging quarterback Geno Smith in a locker room altercation Aug. 12 -- went to midfield for the coin toss.

The Jets and Bills then went out and played the type of sluggish, offensively-challenged game that only Ryan could love. The two teams combined for 12 first half possessions, only two of which yielded more than one first down.

The Jets took a 3-0 lead when new kicker Randy Bullock capped a 10-play, 76-yard drive by booting a 29-yard field goal with 4:01 left in the first quarter.

The Bills took the lead by scoring nine points in nine seconds late in the second quarter. A 47-yard field goal by Carpenter tied the game with 5:47 remaining.

Jets wide receiver Devin Smith fumbled the subsequent kickoff and safety Duke Williams picked up the ball and raced untouched into the end zone for a 19-yard touchdown.

Carpenter's 41-yard field goal with three seconds left extended the lead to 12-3.