Don*_*ela 1 forms hidden http same-origin-policy angular
为了克服提交常规 HTTP 请求时遇到的 CORS(跨源请求共享)问题,我需要在 Angular 4 中提交一个隐藏表单。我在 HTML 中做到了这一点,没有任何问题。但是,我不知道如何在 Angular 中做到这一点。这是我的组件 html 中的代码:
<form form #f="ngForm" action="https://whatever.site.I_access" method="get">
<input type="hidden" name="scope" value="openid email">
<input type="hidden" name="response_type" value="id_token token">
<input type="hidden" name="client_id" value="myClientId">
<input type="hidden" name="redirect_uri" value="https://my.redirect.com/">
<input type="hidden" name="nonce" value="aNonceValue">
<button type="submit" (click)="f.submit()">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)
在我的 .ts 文件中,我实现了“提交”功能。如果将其留空,则不会提交表单。在此函数中编写的命令是什么,只是为了提交具有指定操作的表单?
onSubmit(){
console.log("form submitted");
}
Run Code Online (Sandbox Code Playgroud)
有什么线索吗?
只要将表格附加到文档中,它就应该起作用:
<form #form ngNoForm action="https://whatever.site.I_access" method="get">
<input type="hidden" name="scope" value="openid email">
<input type="hidden" name="response_type" value="id_token token">
<input type="hidden" name="client_id" value="myClientId">
<input type="hidden" name="redirect_uri" value="https://my.redirect.com/">
<input type="hidden" name="nonce" value="aNonceValue">
<button type="submit">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)
这将使 Angular 忽略该表单,因此它将是一个纯 HTML 表单。该按钮将触发其提交。
要以编程方式提交表单,请在组件上获取表单。form
下面指的是#form
上面。nativeElement
指HTMLFormElement
.
import { ..., ElementRef } from '@angular/core';
...
@ViewChild('form') form: ElementRef;
...
submitForm() {
this.form.nativeElement.submit();
}
Run Code Online (Sandbox Code Playgroud)
然后submitForm()
有需要的时候打电话。
归档时间: |
|
查看次数: |
5584 次 |
最近记录: |