I'm using two html select form elements - both with the multiple attribute set. I am attempting to send a $.ajax post request to a FastAPI post method. This api method works for both POSTMAN and using Fastapi DOCs interface. The following is the fastapi snippet and the client side jquery ajax method I'm sending. I'm getting a 422 HTTP Response back from the server and I suspect it has something to do with my formatting things. Any help would be greatly appreciated.
The Fastapi side of things
class FilterModel(BaseModel):
state_abbrev: list[str] = []
card_type: list[str]= []
def get_db():
try:
db = SessionLocal()
yield db
finally:
db.close()
@router.post('/filter')
async def filter_cards(filterModel: FilterModel, db: Session=Depends(get_db)):
state_abbrev = filterModel.state_abbrev
card_type = filterModel.card_type
if state_abbrev:
cards = db.query(models.Cards).filter(models.Cards.state_abbrev.in_(state_abbrev))
if biz_type:
cards = cards.filter(models.Cards.card_type.in_(card_type))
cards = cards.all()
return {"cards":cards}
On the client side
<script>
const form = document.getElementById('map-filter-form');
let states;
let card_types;
$(document).ready(function(){
form.addEventListener('submit', (e) => {
e.preventDefault();
states = JSON.stringify( $('#state_abbrev').val());
card_types = JSON.stringify($('#card_type').val());
data = {"state_abbrev": states, "card_type": card_types};
$.ajax({
url: "http://localhost:8000/mapview/filter",
type: "post",
data: data,
contentType: "application/json",
success: function(rslts) {console.log(rslts)}
});
});
});
</script>