Lee Morgan
Lee Morgan

Reputation: 706

How can I make a cross-site POST request with redirecting?

I am building a site with a list of words for learning a language. I also have another site on a sub-domain for flashcards. I want users to be able to choose words from the list, then press a button that will make a post request to the flashcards site with the list of words chosen and generate a page with the flashcards.

I have already done this with a simple GET request, storing the word data in the URL. For obvious reasons, this is not the greatest way to handle this. Now I am trying to use POST to send the data. Here is the code that I have:

const createCustomDeck = ()=>{
    let items = document.querySelectorAll(".selected");

    let cards = [];
    for(let i = 0; i < items.length; i++){
        cards.push([items[i].children[0].textContent, items[i].children[1].textContent]);
    }

    let form = document.createElement("form");
    form.method = "post";
    form.action = "http://localhost:8001/deck/custom";
    let hidden = document.createElement("input");
    hidden.type = "hidden";
    hidden.name = "cards";
    hidden.value = JSON.stringify(cards);
    form.appendChild(hidden);
    form.submit();
}

This doesn't seem to do anything though. The function is running, I have tested with log statements. On the flashcards server I have CORS set up to accept from any domain (for now). However, it seems that all of this code runs, but the form just does not submit. I checked the network tab and no request is sent.

How do I get this request to send and then redirect to the new page from the cross-site server?

Upvotes: 0

Views: 34

Answers (0)

Related Questions