问题描述
我是Angular 2的新手.这似乎很饿,但是我需要将 form
发送到外部链接,此外,我需要在发布请求后将用户重定向到此页面...
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>
在我像这样通过JQuery发出请求之前,
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>
但是现在我陷入困境,不知道如何在Angular 2中做到这一点.我读了很多主题,但是他们很无助.我尝试了 window.location.href ='...';
,但这仅对获取请求有效,而对发布则毫无用处.任何帮助表示赞赏.
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.
更新我尝试过的(在服务中):
UPDATEWhat 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);
}
在组件中:
submitHiddenForm() {
this._upgradeService.submitHiddenForm().subscribe (data => {
window.location.href = 'http://somelink.com'; },
error => {console.log('Error while redirecting!'); });
}
我有错误:
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.
在我的POST请求中,我有这样的标头:
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);
})
这篇关于Angular 2发布表单到外部链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!