gpt4 book ai didi

javascript - Angular 2 将表单发布到外部链接

转载 作者:行者123 更新时间:2023-11-28 10:48:36 26 4
gpt4 key购买 nike

我是 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 = '...'; 但它仅适用于 Get 请求,对 post 没用。任何帮助表示赞赏。

更新我尝试过的(在服务中):

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 请求中,我有这样的 header :

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;
}

最佳答案

根据post请求的状态码尝试一下

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);
})

关于javascript - Angular 2 将表单发布到外部链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38366652/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com