The Senate voted Thursday to stiffen President Trump’s spine and reassure allies that the U.S. is committed to its military efforts in Syria and Afghanistan, in a vote that tied Democrats in knots.

The non-binding language calls al Qaeda and the Islamic State continuing global threats, contradicting Mr. Trump’s Twitter-based foreign policy claims that the fight against international terrorism has been largely wrapped up.

Instead, the “sense of the Senate” measure urged the president to take a new look at Syria and Afghanistan and come up with a plan for victory without “precipitous withdrawal.”

It drew support in a 68-23 vote that headed off an attempted filibuster.

“It’s not complicated,” said Senate Majority Leader Mitch McConnell, who forced the vote. “Just an opportunity for senators to go on the record about what our country should be doing in Syria and Afghanistan.”

He led all but three Republicans and even a majority of Democrats in backing the language.

But for some of them, it was a complicated vote.

Democratic senators stood in groups on the chamber floor debating each other over what the vote would mean.

Minority Leader Charles E. Schumer, who voted against it, debated Sens. Jack Reed and Robert Menendez, the top Democrats on the Armed Services and Foreign Relations committees, both of whom voted to back the language.

Some worried they were reinvigorating the 2001 authorization to use military force against global terrorism — a piece of legislation that has been stretched to cover American military intervention in more than a dozen countries.

Other senators rejected that.

“Let me be clear: it does not,” said Sen. Dianne Feinstein, who backed the legislation. “This amendment in no way authorizes U.S. forces to remain in Syria or Afghanistan permanently, nor does it alter the existing authorizations in place since 2001. It simply states that the United States should not withdraw until political resolutions are in place.”

Even Democrats’ presidential hopefuls wrestled with what to do, torn between the chance to tweak Mr. Trump and the fear of voting for an amendment that could leave them on record supporting an extended troop commitment.

Sen. Bernard Sanders, Vermont independent, tried to have the best of both worlds, blaming Mr. Trump for a “reckless” foreign policy, but ultimately ending up on the same pro-withdrawal side.

“The American people do not want endless war. It is the job of Congress to responsibly end these military interventions and bring our troops home, not to come up with more reasons to continue them, as this amendment does. That is why I voted against it,” he said.

Mr. Trump late last year declared the U.S. had won its battle against the Islamic State in Syria, and said he would bring troops back. A disagreement over that policy sparked the resignation of Defense Secretary James Mattis.

As of mid-January the Pentagon had pulled back some equipment, though no troops.

Mr. Trump in December also asked the Pentagon to draw up plans for withdrawal from Afghanistan.

The Senate legislation doesn’t compel the president to stay, but it does serve as a strong signal senators are not sold on the president’s direction.

Yet, Sen. John Kennedy, one of the Republicans who voted against the language, said he wasn’t sure who’s right and who’s wrong in the withdrawal debate — and he wasn’t going to back a stay-the-course approach.

“Our Middle East policy right now looks like something my dog’s been keeping under our back porch. Nobody knows what it is, but it’s ugly,” the Louisiana Republican said.

The other Republican “no” votes were Sens. Ted Cruz of Texas and Mike Lee of Utah.

Most Republicans, though, were eager to push for an extended commitment, saying the threats remain.

“No one has forgotten Sept. 11, 2001, but sometimes we fail to remember what made is possible in the first place,” said Sen. Marco Rubio, Florida Republican.