I'm doing a ASP.NET mvc5 project. In one of the pages, I have a button that I want to use to do a post to a cross domain api.
I've tried doing the post like this with ajax:
$.ajax({
type: "POST",
url: URLX,
data: myJSObject,
success: function (data) {
console.log("test123");
},
"async": true,
"crossDomain": true,
headers: {
"Content-Type": "application/json",
//"Authorization": "Bearer XX+n06LhXHb/cAZyBSvXZAd1LlkO8NqtORuHGyexWr4=",
"apiKey": "MEV8yv3hxxxxxxxxxxxxxxxFdKWJer4H3LmL6ntcL",
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Origin": "*"
}
});
but from what I read I can't do a cross domain request with JavaScript, so I tried with a form:
<div class="hiddenform">
<iframe name="hiddenFrame" width="0" height="0" border="0" style="display: none;"></iframe>
<form action="http//something.com/otherthing" id="login_form" method="post" target="hiddenFrame">
<div class="form_section">You can login here</div>
<div class="form_section">
<input xmlns="http://www.w3.org/1999/xhtml" class="text" id="userIdform"
name="session[sec1]" tabindex="1" type="text" value="" />
</div>
<div class="form_section">
<input xmlns="http://www.w3.org/1999/xhtml" class="text" id="periodform"
name="session[sec2]" tabindex="1" type="text" value="" />
</div>
<div class="form_section">
<input xmlns="http://www.w3.org/1999/xhtml" class="text" id="benchform"
name="session[sec3]" tabindex="1" type="text" value="" />
</div>
<div class="form_section">etc</div>
<div xmlns="http://www.w3.org/1999/xhtml" class="buttons">
<button type="submit" class="" name="" id="goform" tabindex="3">Go</button>
</div>
</form>
also, this is the server CORS config https://gist.github.com/michiel/1064640
My problem is that I need to send a header with a apiKey, is that possible in a form? If not, what other options are there that allow me to do a cross domain post when I press a button on my asp.net MVC5 page?
Thanks