[英]Angular 2 Post Form to External Link
我是Angular 2的新用戶。這似乎很繁瑣,但是我需要將form
發送到外部鏈接,而且,發布請求后,我需要將用戶重定向到此頁面...
<form id="Form" method="post" action="http//somelink.com" target="TheWindow">
<input type="hidden" name="linkname" value="someValue" />
</form>
在我通過JQuery發出請求之前,像這樣:
<script>
$( document ).ready(function() {
$( ".btnUpgrade" ).on( "click", function(){
window.open('', 'TheWindow');
document.getElementById('Form').submit();
});
});
</script>
但是現在我陷入困境,不知道如何在Angular 2中做到這一點。我讀了很多主題,但是他們很無助。 我嘗試了window.location.href = '...';
但這僅對獲取請求有用,對發布無用。 任何幫助表示贊賞。
更新我嘗試過的(在服務中):
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請求中,我有這樣的標頭:
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;
}
根據發布請求的狀態碼嘗試此操作
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);
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.