WASHINGTON — In a resounding rebuke, Democrats joined with Republicans Wednesday to hand Barack Obama the first veto override of his presidency, voting overwhelmingly to allow families of Sept. 11 victims to sue Saudi Arabia in U.S. courts for its alleged backing of the attackers.

Both the House and Senate voted decisively to reverse Obama’s decision to scuttle the legislation. Democrats in both chambers abandoned the president in large numbers despite warnings from Obama and top national security officials that flaws in the bill could put U.S. interests, troops, and intelligence personnel at risk.

The Senate vote was 97-1, with only Senate Minority Leader Harry Reid, D-Nev., backing the president. The House vote a few hours later was 348-77, with 123 Democrats rebuffing the president and voting to override. Obama said during a CNN interview that overriding his veto was a mistake that may set a “dangerous precedent.”

Lawmakers said their priority wasn’t Saudi Arabia, but the 9/11 victims and their families who continue to demand justice 15 years after attackers killed nearly 3,000 people in New York, the Washington, D.C., area, and Pennsylvania. Fifteen of the 19 Sept. 11 hijackers were Saudis.

“Overriding a presidential veto is something we don’t take lightly, but it was important in this case that the families of the victims of 9/11 be allowed to pursue justice, even if that pursuit causes some diplomatic discomforts,” said Sen. Chuck Schumer, D-N.Y., a chief sponsor of the bill.

Speaking at a forum in Washington, CIA Director John Brennan said he was concerned about how Saudi Arabia, a key U.S. ally in the Middle East, would interpret the bill. He said the Saudis provide significant amounts of information to the U.S. to help foil extremist plots.

“It would be an absolute shame if this legislation, in any way, influenced the Saudi willingness to continue to be among our best counterterrorism partners,” Brennan said.

On CNN, Obama said that a few lawmakers who backed the bill weren’t aware of its potential impact. He didn’t name them. “And, frankly, I wish Congress here had done what’s hard,” he said. “It was, you know, basically a political vote.”

But Republicans and Democrats said the White House had been slow to respond to the bill and miscalculated lawmakers’ intent to act on the legislation along with the 15th anniversary of the terror attacks. When Obama and senior national security officials such as Defense Secretary Ash Carter finally weighed in, it was too late.

The Senate passed the bill by voice vote in May. The Obama White House then made the mistake of thinking the bill would stall in the Republican-controlled House. In August, 9/11 families pressured Speaker Paul Ryan, R-Wis., while he was on a campaign swing in New York.

On Sept. 9, two days before the 15th anniversary of 9/11, the House passed the bill by voice vote with little debate.

Despite reversing Obama’s decision, a bipartisan group of 28 senators led by Bob Corker, R-Tenn., suggested that defects in the bill could open a legal Pandora’s box, triggering lawsuits from people in other countries seeking redress for injuries or deaths caused by military actions in which the U.S. may have had a role.

Corker, the chairman of Foreign Relations Committee, chided the White House for being outraged over the outcome when the administration did so little to sustain the president’s veto.

“There was zero desire to sit down and talk about a way to get to a better outcome. Zero,” Corker told The Associated Press. “To my knowledge, I don’t know of a call from Obama to a single senator over this.”

In a letter sent Tuesday to Senate Minority Leader Harry Reid, Obama said the bill would erode sovereign immunity principles that prevent foreign litigants “from second-guessing our counterterrorism operations and other actions that we take every day.”

But proponents of the bill dismissed Obama’s concerns as unpersuasive. Sen. John Cornyn of Texas, the Senate’s No. 2 Republican, and other supporters said the bill is narrowly tailored and applies only to acts of terrorism that occur on U.S. soil.

Families of the victims and their attorneys dismissed concerns over the legislation as fearmongering. “We rejoice in this triumph and look forward to our day in court and a time when we may finally get more answers regarding who was truly behind the attacks,” said Terry Strada, national chair of the 9/11 Families & Survivors United for Justice Against Terrorism.

The legislation gives victims’ families the right to sue in U.S. court for any role that elements of the Saudi government may have played in the 2001 attacks. Courts would be permitted to waive a claim of foreign sovereign immunity when an act of terrorism occurred inside U.S. borders, according to the terms of the bill.

Obama vetoed the measure last week, telling lawmakers the bill would make the U.S. vulnerable to retaliatory litigation.

In a separate letter sent Monday to a senior House member, Defense Secretary Ash Carter described the potential for foreign litigants to seek classified intelligence data and analysis and sensitive operational information to establish their cases in what could be an “intrusive discovery process.”

AP Writers Erica Werner and Deb Riechmann contributed to this article.

Follow Richard Lardner on Twitter: http://twitter.com/rplardner