我试图将其中一个表单数据发布到特定服务器,这是CORS禁用,这意味着它只接受POST方法和dis-allowed OPTIONS预检请求,这是完全可以理解的,但其中一个帖子说如果请求是简单请求(POST)内容类型= application / x-www-form-urlencoded,)它不应该创建问题,但我仍然得到错误
来自'qpsxswpoi'的'https://****.****.com/hpm/launchTransnoxScreen.hpm'访问XMLHttpRequest已被CORS政策阻止:没有'Access-Control-Allow-Origin'标题存在于请求的资源上。
--
你能帮我解决一下这个问题吗?我对角度全新,刚刚开始探索。
已经检查过上面的URL
Why is an OPTIONS request sent and can I disable it?
当我们点击按钮时,下面的html示例单独工作正常。
它连接到主机并恢复样本页面。
import { Injectable } from '@angular/core';
import { Patient } from '../model/patient';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class SignupService {
private url: string = "https://stagemc.transnox.com/hpm/launchTransnoxScreen.hpm";
private httpHeadersVar = {
headers: new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'text/html'
})
};
patientFormData: Patient;
constructor(private http: HttpClient) { }
callThP(data) {
const formData = new FormData();
formData.append('myKey1', 'some value 1');
formData.append('myKey2', 'some value 2');
formData.append('myKey3', 'true');
this.http.post(this.url, formData, this.httpHeadersVar).subscribe(
(res) => {
console.log(res);
},
err => console.log(err)
);
}
}
你可以点击这个链接:
<html>
<head></head>
<body>
<form id="hppForm" name="hppForm" method="POST" action=" https://stagecp.transnox.com/hpm/launchTransnoxScreen.hpm">
<input name="uniqueID" id="uniqueID" type="hidden" value="XXX3" />
<input name="deviceID" id="deviceID" type="hidden" value="test_deviceID"/>
<button type="submit" class="btn btn-primary btn-block">Sign up</button>
</form>
</body>
</html>
您还应该检查您在哪里创建API的后端语言,然后启用CORS
我认为HTTP标头不起作用:试试这个:
`实施这个:
如果这不起作用,那么您的API可能不允许CORS。