I'm building a Web App and I am required to make some form of Cross-Domain POST request to a server which I don't personally have access to.
I've tried many things to make it work with Ajax but I found no success. I did a bit of research and eventually I eventually found a solution which was to create an invisible iframe with a form, then submit the said form to get back the information I require.
However, this form is created in javascript, so I use the form.submit() method to submit the said form, and this, rather than simply giving me the JSON file with the information I require, redirects me to the actual JSON file in a different page altogether.
How can I prevent this from happening?
var ifr = document.createElement('iframe');
var frm = document.createElement('form');
frm.setAttribute("id", "invis_form");
frm.setAttribute("action", endpoint);
frm.setAttribute("method", "post");
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("name", "chave");
x.setAttribute("value", apikey);
var y = document.createElement("INPUT");
y.setAttribute("type", "text");
y.setAttribute("name", "valor");
y.setAttribute("value", "125");
frm.appendChild(x);
frm.appendChild(y);
ifr.appendChild(frm);
document.body.appendChild(ifr);
frm.submit();
I was expecting to get something I could print on the console, not to be redirected to a JSON file with the information I need.