I am new in Angular 2. It seems to be starnge, but I need to send form
to external link, moreover, I need to redirect user to this page after post request...
<form id="Form" method="post" action="http//somelink.com" target="TheWindow">
<input type="hidden" name="linkname" value="someValue" />
</form>
Before I made that request by JQuery like that:
<script>
$( document ).ready(function() {
$( ".btnUpgrade" ).on( "click", function(){
window.open('', 'TheWindow');
document.getElementById('Form').submit();
});
});
</script>
But now I'm stuck and no idea how to do that in Angular 2. I read a lot of topics but they been helpless. I tried window.location.href = '...';
but it's good only for Get request and useless for post. Any help appreciated.
UPDATE What I tried(in Service):
submitHiddenForm() {
var headers = new Headers();
headers = this._httpClient.createCustomHeader();
var url = 'http//somelink.com';
var body = 'linkname=someValue';
return this.http.post(url, body, headers).map(this.extractData).catch(this.handleError);
}
In component:
submitHiddenForm() {
this._upgradeService.submitHiddenForm().subscribe (data => {
window.location.href = 'http://somelink.com'; },
error => {console.log('Error while redirecting!'); });
}
I have error:
XMLHttpRequest cannot load http://somelink.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
In my POST request I have such Headers:
createCustomHeader(): Headers {
var headers = new Headers();
headers.append('Access-Control-Allow-Origin', '*');
headers.append('Access-Control-Allow-Headers', 'origin, x-requested-with, x-http-method-override, content-type');
headers.append('Access-Control-Allow-Methods', 'POST');
headers.append('Access-Control-Allow-Credentials', 'true');
return headers;
}
try this on the basis of status code of post request
this.http.request(new Request(this.requestoptions))
.subscribe(res => {
if (res[0].status == 201) {
this.router.navigateByUrl('..URL here..');
}
console.log(res[0].json);
},
err => {
console.log(err);
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.