WASHINGTON—Legislation to provide $40 million a year to improve security in the nation’s schools has bipartisan support, but it was dealt a blow after the Senate rejected an amendment to which it was linked: expanded background checks on gun purchases.